久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 讀取和寫入 int 是原子的嗎?

Are C++ Reads and Writes of an int Atomic?(C++ 讀取和寫入 int 是原子的嗎?)
本文介紹了C++ 讀取和寫入 int 是原子的嗎?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有兩個(gè)線程,一個(gè)更新一個(gè) int 一個(gè)讀取它.這是一個(gè)與讀寫順序無關(guān)的統(tǒng)計(jì)值.

I have two threads, one updating an int and one reading it. This is a statistic value where the order of the reads and writes is irrelevant.

我的問題是,我是否需要同步訪問這個(gè)多字節(jié)值?或者,換句話說,可以部分寫入完成并被中斷,然后讀取發(fā)生.

My question is, do I need to synchronize access to this multi-byte value anyway? Or, put another way, can part of the write be complete and get interrupted, and then the read happen.

例如,考慮一個(gè)值 = 0x0000FFFF,它的增量值為 0x00010000.

For example, think of a value = 0x0000FFFF that gets incremented value of 0x00010000.

有沒有我應(yīng)該擔(dān)心值看起來像 0x0001FFFF 的時(shí)候?當(dāng)然,類型越大,這種事情發(fā)生的可能性就越大.

Is there a time where the value looks like 0x0001FFFF that I should be worried about? Certainly the larger the type, the more possible something like this to happen.

我一直同步這些類型的訪問,但很好奇社區(qū)的想法.

I've always synchronized these types of accesses, but was curious what the community thinks.

推薦答案

起初人們可能認(rèn)為對(duì)本地機(jī)器大小的讀取和寫入是原子的,但有許多問題需要處理,包括處理器/內(nèi)核之間的緩存一致性.在 Windows 上使用 Interlocked* 等原子操作,在 Linux 上使用等效操作.C++0x 將有一個(gè)原子"模板來將它們包裝在一個(gè)漂亮的跨平臺(tái)界面中.現(xiàn)在,如果您使用的是平臺(tái)抽象層,它可能會(huì)提供這些功能.ACE 可以,請(qǐng)參閱類模板 ACE_Atomic_Op.

At first one might think that reads and writes of the native machine size are atomic but there are a number of issues to deal with including cache coherency between processors/cores. Use atomic operations like Interlocked* on Windows and the equivalent on Linux. C++0x will have an "atomic" template to wrap these in a nice and cross-platform interface. For now if you are using a platform abstraction layer it may provide these functions. ACE does, see the class template ACE_Atomic_Op.

這篇關(guān)于C++ 讀取和寫入 int 是原子的嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對(duì)壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識(shí)別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對(duì)列表進(jìn)行排序)
主站蜘蛛池模板: 亚洲国产精品久久久 | 精品国产青草久久久久96 | 一区二区三区电影网 | 午夜无码国产理论在线 | 国产高清在线观看 | 国产不卡一区 | 国精品一区 | 亚洲综合一区二区三区 | 香蕉视频1024 | 亚洲一区二区高清 | 中文字幕日韩一区二区 | 国产高清精品一区二区三区 | 国产精久久久久久久 | 精品视频一区二区三区 | 久久久五月天 | 久久国产亚洲 | 91久久精品国产91久久 | 国产成人福利视频 | 国产欧美一区二区三区日本久久久 | www.中文字幕.com | 在线91| 成人片在线看 | 久久午夜视频 | 日韩一区二区三区在线观看 | 性色视频 | 蜜桃av一区二区三区 | 国产精品色av | 久久噜噜噜精品国产亚洲综合 | 日韩av一区二区在线观看 | 亚洲人在线 | 精品视频在线观看 | 国产精品污www一区二区三区 | 97caoporn国产免费人人 | 亚洲免费人成在线视频观看 | 91视频.| 99精品国产一区二区青青牛奶 | 亚洲精品中文字幕在线观看 | 欧美日韩国产一区二区三区 | 久久久久久国产精品久久 | 日韩在线免费视频 | 成人免费视频在线观看 |