Threads

A kind of lightweight process that shares the same address space. Context switching is also less expensive

Every thread has its own, PC, registers and stack kept in the thread table.

Joining: a thread blocks for another thread to finish

Yielding: a thread gives up the CPU to allow another thread to run

Remember the difference between concurrency and parallelism:

  • Concurrency: the ability of a system to handle multiple tasks or processes simultaneously. These tasks may not necessarily be executing at the same time, e.g. the CPU switches between the tasks.
  • Parallelism: the simultaneous execution of multiple tasks or processes, where each task is executed concurrently on separate CPU to achieve faster computation. Note that threads are not necessarily parallel!

User vs kernel mode treads #

  • User threads
    • Advantages:
      • Implemented by thread library in user space, don't need kernel support
      • Fast thread switch (no need to TRAP kernel) & applications can customize scheduling
    • Disadvantages:
      • Blocking syscalls will block ALL threads
      • Page faults
      • Threads must voluntarary give up CPU
  • Kernel threads: Implemented by operating system
    • Advantage: Kernel can switch to other threads on blocking syscalls
    • Disadvantage: overhead on thread operation syscalls
    • One-to-one: maps one user thread to one kernel thread
      • Overhead when creating kernel threads
    • Many-to-one: many user-level threads on one kernel thread
    • Many-to-many: user threads are multiplexed on kernel threads

Threads introduce a lot of extra complexity with issues that arise when single threaded programs are converted to multi-threaded programs relating to global variables, signals, different stacks that must be properly handled.

Event-driven #

In event-driven programming, even threads are considered too expensive, instead the program uses a finite-state machine where each computation has a saved state and can be switched between.

  • Use non-blocking system calls: epoll on Linux, kqueue on FreeBSD
  • Solve the C10k problem: web servers can easily handle 10,000 connections