How to decide whether to use threads or create separate process altogether in your application to achieve parallelism.
A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler. Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes.
You'd prefer multiple threads over multiple processes for two reasons: Inter-thread communication (sharing data etc.) is significantly simpler to program than inter-process communication. Context switches between threads are faster than between processes.
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.
Processes have more isolated memory. This is important for a number of reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With