Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
236
|
第七章
如果是以上的結果,我們很幸運。但也可能出現這樣的結果:
時間 執行緒 1 執行緒 2
1 load x into register (5)
2 add 1 to register (6)
3 load x into register (5)
4 store register in x (6)
5 add 1 to register (6)
6 store register in x (6)
許多其他的組合也會導致意想不到的結果。這些例子所展現的是並行性而非平行性。就
平行性而言,執行緒可以同時執行,這會帶來更危險的組合:
時間 執行緒 1 執行緒 2
1 load x into register (5) load x into register (5)
2 add 1 to register (6) add 1 to register (6)
3 store register in x (6) store register in x (6)
明白了嗎。即使簡單如把 1 加入變數的操作— 1 列簡單的 C C++ 程式碼—只要我們
同時執行了許多執行緒就會伴隨著爭用。甚至不需要平行性。單處理器機器就會—並且
很可能會—遇到這些爭用的情況。爭用情況是讓程式設計者受挫及程式出現瑕疵的最大
源頭。讓我們來看看程式設計者處理它們的方式。
同步
爭用情況的基本源頭是關鍵區,關鍵區就是為了使得程式有正確的行為不讓執行緒交替
執行的一個範圍。因此,為了避免出現爭用的情況,程式設計者需要以同步的方式來存
取該範圍,以確保關鍵區的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812