代码改变世界

golang 并发 vs 并行 (Concurrency Is Not Parallelism)

2022-03-13 15:10  youxin  阅读(63)  评论(0编辑  收藏  举报

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