when to use process v/s thread?
I know the theoretical difference between the thread and proce开发者_如何学运维ss. But in practical when to use the thread and process because both will does the same work.
In general (and it varies by operating system):
- Threads are usually lighter-weight than processes
- Processes provide better isolation between actions
- Threads provide simpler data sharing and coordination within the process
Typically the middle point is the kicker for me - if you really, really don't want two actions to interfere with each other, to the extent that one process going belly-up doesn't affect the other action, use separate processes. Otherwise I personally go for threads.
(I'm assuming that both models are available - if you want to run a separate executable, that's going to be pretty hard to do within an existing thread, at least in most environments I'm aware of.)
Thread is a subtotal of a process. Hereby the main difference is memory allocation and CPU time scheduling:
- operating system handles memory per process and schedules execution time for processes
- you allocate memory (within the bounds allowed per process) and you schedule execution time (within given execution timeframe per process) for threads
Other than that there's a lot of minor defining differences, like hardware allocation (threads can share hardware locked by their process), communication (depending on the platform/language/runtime, threads can share variables, processes need a pipe to share information) etc. There's much more in this distinction if you think of a thread as of an atomic entity, whilst process in that case would be the way to group these entities.
精彩评论