Concurrency: Processes vs Threads

后端 未结 4 669
悲哀的现实
悲哀的现实 2021-02-18 23:46

What are the main advantages of using a model for concurrency based on processes over one based on threads and in what contexts is the latter appropriate?

4条回答
  •  甜味超标
    2021-02-19 00:09

    The disadvantage of using a process-based model is that it will be slower. You will have to copy data between the concurrent parts of your program.

    The disadvantage of using a thread-based model is that you will probably get it wrong. It may sound mean, but it's true-- show me code based on threads and I'll show you a bug. I've found bugs in threaded code that has run "correctly" for 10 years.

    The advantages of using a process-based model are numerous. The separation forces you to think in terms of protocols and formal communication patterns, which means its far more likely that you will get it right. Processes communicating with each other are easier to scale out across multiple machines. Multiple concurrent processes allows one process to crash without necessarily crashing the others.

    The advantage of using a thread-based model is that it is fast.

    It may be obvious which of the two I prefer, but in case it isn't: processes, every day of the week and twice on Sunday. Threads are too hard: I haven't ever met anybody who could write correct multi-threaded code; those that claim to be able to usually don't know enough about the space yet.

提交回复
热议问题