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

前端 未结 8 1628
南旧
南旧 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:13

    I agree to most of the answers above. But speaking from design perspective i would rather go for a thread when i want set of logically co-related operations to be carried out parallel. For example if you run a word processor there will be one thread running in foreground as an editor and other thread running in background auto saving the document at regular intervals so no one would design a process to do that auto saving task separately.

    0 讨论(0)
  • 2020-12-12 14:17

    In addition to the other answers, maintaining and deploying a single process is a lot simpler than having a few executables.

    One would use multiple processes/executables to provide a well-defined interface/decoupling so that one part or the other can be reused or reimplemented more easily than keeping all the functionality in one process.

    0 讨论(0)
  • 2020-12-12 14:19

    You'd prefer multiple threads over multiple processes for two reasons:

    1. Inter-thread communication (sharing data etc.) is significantly simpler to program than inter-process communication.
    2. Context switches between threads are faster than between processes. That is, it's quicker for the OS to stop one thread and start running another than do the same with two processes.

    Example:

    Applications with GUIs typically use one thread for the GUI and others for background computation. The spellchecker in MS Office, for example, is a separate thread from the one running the Office user interface. In such applications, using multiple processes instead would result in slower performance and code that's tough to write and maintain.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 14:30

    I assume you already know you need a thread or a process, so I'd say the main reason to pick one over the other would be data sharing.

    Use of a process means you also need Inter Process Communication (IPC) to get data in and out of the process. This is a good thing if the process is to be isolated though.

    0 讨论(0)
  • 2020-12-12 14:37

    I would like to answer this in a different way. "It depends on your application's working scenario and performance SLA" would be my answer.

    For instance threads may be sharing the same address space and communication between threads may be faster and easier but it is also possible that under certain conditions threads deadlock and then what do you think would happen to your process.

    Even if you are a programming whiz and have used all the fancy thread synchronization mechanisms to prevent deadlocks it certainly is not rocket science to see that unless a deterministic model is followed which may be the case with hard real time systems running on Real Time OSes where you have a certain degree of control over thread priorities and can expect the OS to respect these priorities it may not be the case with General Purpose OSes like Windows.

    From a Design perspective too you might want to isolate your functionality into independent self contained modules where they may not really need to share the same address space or memory or even talk to each other. This is a case where processes will make sense.

    Take the case of Google Chrome where multiple processes are spawned as opposed to most browsers which use a multi-threaded model. Each tab in Chrome can be talking to a different server and rendering a different website. Imagine what would happen if one website stopped responding and if you had a thread stalled due to this, the entire browser would either slow down or come to a stop. So Google decided to spawn multiple processes and that is why even if one tab freezes you can still continue using other tabs of your Chrome browser.

    Read more about it here
    and also look here

    0 讨论(0)
提交回复
热议问题