漫談並發和並行

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

0x00 前言

比較擔心自己最終有一天會陷入對各種工具的使用,而忽視了對一些基礎知識的學習。因此,開始系列地整理一些知識。

本文關注並發和並行,雖說是漫談,其實都是看書看知乎看各種文章,理論基本也都是湊出來的。我只是做了搬運工+自己的一丁點理解。

文章結構

  • 概述,大致描述一下並發和並行的區別
  • 摘錄了兩個關於並行和並發的區別,英語的那一段寫的十分好。
  • 列出來了4種並行的架構
  • 放一個c++的多線程的例子

0x01 概述

並發是同一時間應對(dealing with)多件事情的能力!並行是同一時間動手做(doing)多件事情的能力! ——Rob Pike

那麼我們該怎麼理解什麼是並發?什麼是並行呢?

  • 並發:並發程式含有多個邏輯上的獨立執行塊,它們可以獨立地並存執行,也可以穿行執行。注意獨立這個詞,它對我們理解這些概念很重要。
  • 並行:並行程式解決問題的速度往往比串列程式快的多,因為其可以同時執行整個任務的多個部分,並行程式可能有多個獨立執行塊,也可能僅有一個?

換一個角度來看它們的差異:並發是問題域中的概念——要被設計成能夠處理多個同時發生的事件;而並行則是方法域中的概念——通過將問題中的多個部分並存執行,來加速問題。

0x02 摘錄

摘錄一些其它渠道得來的回到。

摘錄1(來自知乎:知乎使用者)

  • 並發(Concurrency)是同時處理很多事情(dealing with lots of things at once),並行(Parallelism)是同時執行很多事情(doing lots of things at once);
  • 二者有相關度,但並非同一個概念:並發可認為是一種邏輯結構的設計模式。你可以用並發的設計方式去設計模型,然後運行在一個單核系統上,通過系統動態地邏輯切換製造出並行的假象。此時,你的程式不是並行,但是是並發的。你可以將這種模型不加修改地運行在多核系統上,此時你的程式可以認為是並行。此處,並行更關注的是程式的執行(execution);
  • 在電腦中,我們通常會引入獨立的運行實體來對並行存取模型的建模型,如:
    • 作業系統層級的進程和線程;
    • 程式設計語言內建的並發實體概念:
      • 如Golang 中的 goroutine(CSP 模型);
      • Erlang 中的 process(Actor 模型);
  • 現實世界是並行的,人腦也是並行的。

摘錄2(來自國外友人)

這個回答的十分好,很值得仔細看一遍。

The terms concurrency and parallelism are often used in relation to multithreaded programs. But what exactly does concurrency and parallelism mean, and are they the same terms or what?

The short answer is "no". They are not the same terms, although they appear quite similar on the surface. It also took me some time to finally find and understand the difference between concurrency and parallelism. Therefore I decided to add a text about concurrency vs. parallelism to this Java concurrency tutorial.

Concurrency

Concurrency means that an application is making progress on more than one task at the same time (concurrently). Well, if the computer only has one CPU the application may not make progress on more than one task at exactly the same time, but more than one task is being processed at a time inside the application. It does not completely finish one task before it begins the next.

Parallelism

Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time.

Concurrency vs. Parallelism In Detail

As you can see, concurrency is related to how an application handles multiple tasks it works on. An application may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently).

Parallelism on the other hand, is related to how an application handles each individual task. An application may process the task serially from start to end, or split the task up into subtasks which can be completed in parallel.

As you can see, an application can be concurrent, but not parallel. This means that it processes more than one task at the same time, but the tasks are not broken down into subtasks.

An application can also be parallel but not concurrent. This means that the application only works on one task at a time, and this task is broken down into subtasks which can be processed in parallel.

Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at a time, and the task is never broken down into subtasks for parallel execution.

Finally, an application can also be both concurrent and parallel, in that it both works on multiple tasks at the same time, and also breaks each task down into subtasks for parallel execution. However, some of the benefits of concurrency and parallelism may be lost in this scenario, as the CPUs in the computer are already kept reasonably busy with either concurrency or parallelism alone. Combining it may lead to only a small performance gain or even performance loss. Make sure you analyze and measure before you adopt a concurrent parallel model blindly.

0x03 並行架構

1.位級(bit-level)並行

為什麼32位電腦比8位電腦運行速度更快?因為並行。對於兩個32位元的加法,8位電腦必須進行多次8位計算,而32位電腦可以一步完成,即並行地處理32位元的4位元組。電腦的發展經曆了8位、16位、32位,現在正處於64位時代。然而由位升級帶來的效能改 善是存在瓶頸的,這也正是短期內我們無法步入128位時代的原因。

2.指令級(instruction-level)並行

現代CPU的並行度很高,其中使用的技術包括流水線、亂序執行和猜測執行等。程式員通常可以不關心處理器內部並行的細節,因為儘管處理器內部的並行度很高,但是經過精心設計,從外部看上去所有處理都像是串列的。而這種“看上去像串列”的設計逐漸層得不適用。處理器的設計者們為單核提升速度變得越來越困難。

進入多核時代,我們必須面對的情況是:無論是表面上還是實質上,指令都不再串列執行了。

3.資料級(data-level)並行

並行地在大量資料上施加同一操作。這並不適合解決所有問題,但在適合的情境卻可以大展身手。 影像處理就是一種適合進行資料級並行的情境。比如,為了增加圖片亮度就需要增加每一個像素的亮度。現代GPU(圖形處理器)也因影像處理的特點而演化成了極其強大的資料平行處理器。

4.任務級(task-level)並行

從程式員的角度來看,多處理器架構最明顯的分類特徵是其記憶體模型(共用記憶體模型或分布式記憶體模型)。

  • 對於共用記憶體的多處理器系統,每個處理器都能訪問整個記憶體,處理器之間的通訊主要通過記憶體進行。
  • 對於分布式記憶體的多處理器系統,每個處理器都有自己的記憶體,處理器之間的通訊主要通過網路進行。

通過記憶體通訊比通過網路通訊更簡單更快速,所以用共用記憶體編程往往更容易。然而,當處理器個數逐漸增多,共用記憶體就會遭遇效能瓶頸——此時不得不轉向分布式記憶體。如果要開發一個容錯系統,就要使用多台電腦以規避硬體故障對系統的影響,此時也必須藉助於分布式記憶體。

0x04 舉個栗子

前面都是理論,這就放一個極簡單的c++的多線程的例子吧。程式這麼簡單就不再講了。

#include <pthread.h>#include "stdio.h"using namespace std;#define NUM_THREADS 3// 線程的運行函數void* say_hello(void* args){    printf ("Hello Dante! You're Great!\n");}int main(){    // 定義線程的 id 變數,多個變數使用數組    pthread_t tids[NUM_THREADS];    for(int i = 0; i < NUM_THREADS; ++i)    {        //參數依次是:建立的線程id,線程參數,調用的函數,傳入的函數參數        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);        printf ("Hello Dante! You're Gorgeous!\n");        if (ret != 0)        {           printf ("pthread_create error: error_code=%d \n", ret);        }    }    //等各個線程退出後,進程才結束,否則進程強制結束了,線程可能還沒反應過來;    pthread_exit(NULL);}

運行結果如下,注意一下列印的結果,第二次列印的和其餘的不一樣?為什麼不一樣,就不補充了。

dante@DESKTOP-AE2RHL0:/mnt/d/workspace/c++$ ./a.outHello Dante! You're Great!Hello Dante! You're Gorgeous!Hello Dante! You're Gorgeous!Hello Dante! You're Great!Hello Dante! You're Gorgeous!Hello Dante! You're Great!

0x05 總結

並發編程還是有很多要學的,而且不同語言對於並發編程的支援各有不同,後序我會對各個並行存取模型進行總結和整理,通過不同語言的樣本來說明。

0xFF 參考

  • github地址:https://github.com/dantezhao/concurrency_and_parallelism/blob/master/simple_cpp_example/hello_world.cc
  • 知乎回答:https://www.zhihu.com/question/33515481/answer/135306366
  • Jakob Jenkov:http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html
  • 《七天七並行存取模型》

作者:dantezhao |簡書 | CSDN | GITHUB

文章地址:http://www.jianshu.com/p/930903b35588
個人首頁:http://www.jianshu.com/u/2453cf172ab4
文章可以轉載, 但必須以超連結形式標明文章原始出處和作者資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.