Process vs Threads

前端 未结 6 385
余生分开走
余生分开走 2021-01-31 18:21

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

6条回答
  •  野的像风
    2021-01-31 19:02

    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.

提交回复
热议问题