avatar

Catalog
About Processes,Threads,Coroutine

1.Parallel and Concurrent

  • Parallel: simultaneous execution of multiple tasks
  • Concurrent Multiple tasks are executed alternately on the same processor

2.Processes

(^ω^)
mean program in progress

strengths and weaknesses

strengths:

  • more stability and safety

weaknesses:

  • require more resource overhead when processe swiching
  • IPC is complex and time-consuming

3.Threads

(^ω^)
Thread contained in process,also called lightweight process

if Processes as a hight speed road, threads like the car, they make one processes can execute multiple task

strengths and weaknesses

strengths:

  • require less resource overhead when threads swiching

weaknesses:

  • not stable enough,data might be lost,deadlock

4.Coroutine

coroutine is lightweight threads
(^ω^)

php
1
2
3
4
5
6
7
8
9
10
11
function echoTimes($msg, $max) {
for ($i = 1; $i <= $max; ++$i) {
echo "$msg iteration $i\n";
yield;
}
}

function task1()
{
yield from echoTimes('bar', 5);
}

strengths:

  • high execution efficiency
  • shared resource,no need lock

for multi-core cpu,use multiple processes and coroutine,can get better performance

Author: Qin
Link: https://qinhaolei.com/posts/55598/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Reward
  • WechatPay
    WechatPay
  • AliPay
    AliPay
  • Bitcoin
    Bitcoin

Comment