views:

3881

answers:

7
+9  Q: 

Process vs Threads

How to decide whether to use threads or create seprate process altogether in your application to achieve parallelism.

+1  A: 

Generally you should use processes when the individual execution streams don't need to share global data and you would like to have each protected from the other.

Matt Brandt
+2  A: 

The degree of parallelism mainly depends on the physical processors / cores available on your machine. If you have a single-processor/core machine, then having seperate processes may cause too much overhead. Threads would generally be preferred in that case.

If you have multiple cores/CPUs then depending on what each process/thread does, you may opt for processes if the overhead is justified. Processes obviously have a much better level of memory isolation than threads - but at the same time in Windows, processes are fairly heavy, compared to threads.

Threads of course can share data in the same process - but again you would need to synchronize access to the shared data - to prevent corrupt state. Sharing data between processes is more involved, the overhead (which is greated than simple thread synchronization) depending on the mechanisms used such as Named pipes, custom sockets-based communication, using a remoting framework, shared file / database etc.

Krishna
A: 

In Windows, processes are heavier to create then threads. So if you have several smaller tasks a thread or thread pool would be better. Or use a process pool to recycle the processes. Also sharing state between processes is more work then sharing state between threads. But then again: Threads could destabilize a complete process taking other threads down with it. If you want to minimize the chance of that happening you could go for separate processes. .Net's AppDomains might be a middle ground between both.

Lars Truijens
+6  A: 

Threads are more light weight, and for the making several "workers" just to utilize all availabe CPUs or cores, you're better of with threads.

When you need the workers to be better isolated and more robust, like with most servers, go with sockets. When one thread crashes badly, it usually takes down the entire process, including other threads working in that process. If a process turns sour and dies, it doesn't touch any other process, so they can happily go on with their bussiness as if nothing happened.

wvdschel
+2  A: 

Processes have more isolated memory. This is important for a number of reasons:

  • It is harder for a single task to crash the other tasks.
  • More memory will be available per process. This is important for large, high-performance applications like Apache or database servers, like Postgres. This is important for both allocated memory and memory mapped files.
Adam Tegen
A: 

I liked this explanation: Thread vs. Process

jporter