golang 併發 vs 並行 (Concurrency Is Not Parallelism)
Rob pike發表過一個有名的演講《Concurrency is not parallelism》(https://blog.golang.org/concurrency-is-not-parallelism),
演講膠片在talks.golang.org中可以找到(https://talks.golang.org/2012/waza.slide#1),
演講視訊地址 :https://vimeo.com/49718712
The modern world is parallel
Multicore.
Networks.
Clouds of CPUs.
Loads of users.
Our technology should help.
That's where concurrency comes in.
Go supports concurrency
Go provides:
- concurrent execution (goroutines)
- synchronization and messaging (channels)
- multi-way concurrent control (select)
Concurrency is cool! Yay parallelism!!
NO! A fallacy.
When Go was announced, many were confused by the distinction.
"I ran the prime sieve with 4 processors and it got slower!"
fallacy:
n. 謬論,謬見;推理謬誤;謬誤性
Concurrency
Programming as the composition of independently executing processes.
(Processes in the general sense, not Linux processes. Famously hard to define.)
Parallelism
Programming as the simultaneous execution of (possibly related) computations.
Concurrency vs. parallelism
Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.
Not the same, but related.
Concurrency is about structure, parallelism is about execution.
Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.
An analogy
Concurrent: Mouse, keyboard, display, and disk drivers.
Parallel: Vector dot product.
analogy:
n. 類比,比擬;用類比方法,進行比照;同功
Concurrency plus communication
Concurrency is a way to structure a program by breaking it into pieces that can be executed independently.
Communication is the means to coordinate the independent executions.
This is the Go model and (like Erlang and others) it's based on CSP:
C. A. R. Hoare: Communicating Sequential Processes (CACM 1978)
併發模型中的csp模型:https://www.zhihu.com/question/26192499
參考;https://blog.csdn.net/abccheng/article/details/50913795