Why should I use a thread vs. using a process?

前端 未结 8 1627
南旧
南旧 2020-12-12 13:36

Separating different parts of a program into different processes seems (to me) to make a more elegant program than just threading everything. In what scenario would it make

8条回答
  •  有刺的猬
    2020-12-12 14:29

    You sure don't sound like a newbie. It's an excellent observation that processes are, in many ways, more elegant. Threads are basically an optimization to avoid too many transitions or too much communication between memory spaces.

    Superficially using threads may also seem like it makes your program easier to read and write, because you can share variables and memory between the threads freely. In practice, doing that requires very careful attention to avoid race conditions or deadlocks.

    There are operating-system kernels (most notably L4) that try very hard to improve the efficiency of inter-process communication. For such systems one could probably make a convincing argument that threads are pointless.

提交回复
热议问题