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

MultithreadingProcess

Multithreading Problem Overview


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 sense to make things run on a thread vs. separating the program into different processes? When should I use a thread?


Edit

Anything on how (or if) they act differently with single-core and multi-core would also be helpful.

Multithreading Solutions


Solution 1 - Multithreading

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.

Solution 2 - Multithreading

Well apart from advantages of using thread over process, like:

Advantages:

  • Much quicker to create a thread than a process.
  • Much quicker to switch between threads than to switch between processes.
  • Threads share data easily

Consider few disadvantages too:

  • No security between threads.
  • One thread can stomp on another thread's data.
  • If one thread blocks, all threads in task block.

As to the important part of your question "When should I use a thread?"

Well you should consider few facts that a threads should not alter the semantics of a program. They simply change the timing of operations. As a result, they are almost always used as an elegant solution to performance related problems. Here are some examples of situations where you might use threads:

  • Doing lengthy processing: When a windows application is calculating it cannot process any more messages. As a result, the display cannot be updated.
  • Doing background processing: Some tasks may not be time critical, but need to execute continuously.
  • Doing I/O work: I/O to disk or to network can have unpredictable delays. Threads allow you to ensure that I/O latency does not delay unrelated parts of your application.

Solution 3 - Multithreading

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.

Solution 4 - Multithreading

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.

Solution 5 - Multithreading

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

Solution 6 - Multithreading

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.

Solution 7 - Multithreading

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.

Solution 8 - Multithreading

Came across this post. Interesting discussion. but I felt one point is missing or indirectly pointed.

Creating a new process is costly because of all of the data structures that must be allocated and initialized. The process is subdivided into different threads of control to achieve multithreading inside the process.

Using a thread or a process to achieve the target is based on your program usage requirements and resource utilization.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanmineView Question on Stackoverflow
Solution 1 - MultithreadingFrederick The FoolView Answer on Stackoverflow
Solution 2 - MultithreadingsimplyharshView Answer on Stackoverflow
Solution 3 - MultithreadingBenBView Answer on Stackoverflow
Solution 4 - MultithreadingflodinView Answer on Stackoverflow
Solution 5 - MultithreadingpcodexView Answer on Stackoverflow
Solution 6 - MultithreadingSanjayView Answer on Stackoverflow
Solution 7 - MultithreadingTimView Answer on Stackoverflow
Solution 8 - MultithreadingSureshView Answer on Stackoverflow