Why use threads?

TLDR

We still want to perform concurrent programming.

However, Processes are expensive and Operating Process Communication is difficult!

We wanted a quick hack to deal with these problems.

Pros

Economical

Multiple threads in the same process requires much less resources to manage compared to multiple processes

Resource sharing

  • Threads share most of the resources of a process
  • No need for additional mechanism for passing information around

Responsiveness

  • Multithreaded programs much more responsive

Scalability

  • Multithreaded program can take advantage of multiple CPUs

Cons

  • syscall concurrency

    • parallel execution of multiple threads -> parallel syscall.
    • need to ensure correctness, since syscall is effectful.
  • process behaviour

    • impact on process operations
    • fork() duplicates a process, how about its threads?
    • exit() executes exit(), how about the whole process?
    • exec() single thread calls it, what about other threads?