Multithreading and process

What factors will you look for, choose the implementation of the function as multi-threaded and use one separate process.

0


source to share


5 answers


What is the target OS: On Unix, I'm more likely to think in processes

What language am I writing in: In languages ​​with good threading support, I most likely want threads



How much isolation do I want

How easy is it to use interprocess communication in my target language

+2


source


How many resources do you need? Shared memory is free for threads, but subject to common resource contention issues.



Message passing (considering MPI?) And shared memory. Also consider the task timing and the process of injecting overhead into the start and start streams.

+2


source


I personally consider threads as much as possible, avoiding as much as possible, preferring multi-process solutions as much as possible. The difference, however, lies in how shared the work units involved are.

On Windows with no fork (), I am more likely to use a multi-threaded model than a multi-process model for instances of a variant of the same worker (e.g. Apache handlers or agent processes, which are more than one thing but work similarly in others relationship). On Unix, I would be more inclined to use multiprocessor for almost everything but small worker threads (especially in GUIs to keep the interface responsive). In cross-platform work I use multithreading due to Windows limitations.

+1


source


In this post, I use the word task to represent a process or thread to make the language easier.

What OS?
On Windows, threads are simpler, and on * nix, the multiprocessor is slightly simpler. Ultimately though, you can do both.

Do I need to share data between each task being executed
If I need to constantly exchange data between each task being executed, I would probably go with threads. It's just easier to communicate across threads than with processes, since they all have access to the same chunk of memory. If there are other compelling reasons for using processes, I want to see if I have a good implementation of shared memory available on the platform (like boost :: interprocess).

Mitigating Interference Between Tasks
If it is possible that one of the tasks could fail, I tend to lean towards multiprocess. This is Chrome's approach (one process per tab). This way, if one process crashes, the rest can continue working. If the thread crashes, you will obviously lose all other threads when the whole process goes down.

Load / Pools / Performance
How heavy will the load be? If it is very heavy, I can load the balance between the machines. If I had designed it to be multiprocessor rather than multi-threaded, that would make balancing between machines easier. If I want a pool of tasks that are readily available at any time, then threads are (in a way) simpler since thread pool implementations already exist.

Intangible
What does my gut tell me? Justify this later and hope it is correct! :)

0


source


I think it all depends on the communication, generally between thread communication and synchronization (think mutexes thinking shared memory) is much easier than inter-process communication (think sockets, think networks, think pipes, think mailboxes ), so when you logically group functionality I would go for topics.

However, when you need to have an application that scales cross systems (I think is distributed) or has a lot of correct locking, or where the separation between objects is much stricter than just logical grouping, I would go for the process.

Personally, I would need a very good reason to use multiple processes instead of multithreading.

0


source







All Articles