59

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.

8 Answers 8

92

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.

7
  • 9
    the data sharing is a two-sided coin, though. You can easily mess up the other thread's data...
    – Thilo
    Mar 6, 2009 at 6:14
  • 2
    Does the speed advantage also apply to multi-core? Does the typical OS usually allow for this?
    – danmine
    Mar 6, 2009 at 6:16
  • 3
    As thumb rule, you should not try to predict the OS scheduling behaviour. You can't be sure whether your threads are gonna run on separate cores of their own or on the same core. The same holds for multiple processes. So don't count on this multi-core thing.
    – G S
    Mar 6, 2009 at 6:21
  • 1
    @Frederick: While you're right in general, you CAN be sure on which cores your threads are gonna run, because (at least on Windows) you can set the processor affinity mask. Is/was useful for timing purposes, where changing the core could result in jumps or (worse) the time going back. Mar 6, 2009 at 7:51
  • 4
    @Cannonade: "Multi-threading can be dangerous for the initiated". More true that you intended! I'm sure you mean "UN-initiated" but it's also dangerous for the initiated. Jul 13, 2009 at 15:10
40

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.
4
  • 10
    Are you sure that when 1 thread blocks they all block? What do you mean by block? Like blocked waiting for i/o? What type of threads are you using? I know that PThreads won't block. Mar 6, 2009 at 6:45
  • 4
    Like block waiting for resources. Many people hate it but threads are not isolated as they don't have their own address space. The error cause by a thread can kill the entire process or program because that error affects the entire memory space of all threads use in that process or program. Block !! Mar 6, 2009 at 7:40
  • Thanks for stating the disadvantages Apr 25, 2016 at 9:05
  • seconding @simplyharsh. Consider the case when threads of the same process deadlock, your process is as good as dead.
    – pcodex
    Dec 18, 2019 at 7:12
7

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.

1
  • I like the isolation aspect of processes.
    – danmine
    Mar 6, 2009 at 6:19
7

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.

7

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

3

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.

2

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

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.

1
  • 2
    I might be wrong, but I think in Unix systems the fork command duplicates a process without duplicating its address space (until one of the processes writes to it then it duplicates only the part that was written) so it's not that expensive.
    – frankelot
    Dec 25, 2018 at 18:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.