pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

并發(fā):C++11 內(nèi)存模型中的原子性和易失性

Concurrency: Atomic and volatile in C++11 memory model(并發(fā):C++11 內(nèi)存模型中的原子性和易失性)
本文介紹了并發(fā):C++11 內(nèi)存模型中的原子性和易失性的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

一個(gè)全局變量在 2 個(gè)不同內(nèi)核上的 2 個(gè)并發(fā)運(yùn)行線程之間共享.線程寫(xiě)入和讀取變量.對(duì)于原子變量,一個(gè)線程可以讀取一個(gè)陳舊的值嗎?每個(gè)內(nèi)核可能在其緩存中都有一個(gè)共享變量的值,當(dāng)一個(gè)線程寫(xiě)入緩存中的副本時(shí),不同內(nèi)核上的另一個(gè)線程可能會(huì)從其自己的緩存中讀取過(guò)時(shí)的值.或者編譯器執(zhí)行強(qiáng)內(nèi)存排序以從另一個(gè)緩存中讀取最新值?c++11 標(biāo)準(zhǔn)庫(kù)有 std::atomic 支持.這與 volatile 關(guān)鍵字有何不同?在上述場(chǎng)景中,volatile 和 atomic 類型的行為有何不同?

A global variable is shared across 2 concurrently running threads on 2 different cores. The threads writes to and read from the variables. For the atomic variable can one thread read a stale value? Each core might have a value of the shared variable in its cache and when one threads writes to its copy in a cache the other thread on a different core might read stale value from its own cache. Or the compiler does strong memory ordering to read the latest value from the other cache? The c++11 standard library has std::atomic support. How this is different from the volatile keyword? How volatile and atomic types will behave differently in the above scenario?

推薦答案

首先,volatile 并不意味著原子訪問(wèn).它專為諸如內(nèi)存映射 I/O 和信號(hào)處理之類的事情而設(shè)計(jì).volatilestd::atomic 一起使用時(shí)完全沒(méi)有必要,除非您的平臺(tái)另有說(shuō)明,否則 volatile 與原子訪問(wèn)或內(nèi)存排序無(wú)關(guān)線程之間.

Firstly, volatile does not imply atomic access. It is designed for things like memory mapped I/O and signal handling. volatile is completely unnecessary when used with std::atomic, and unless your platform documents otherwise, volatile has no bearing on atomic access or memory ordering between threads.

如果您有一個(gè)在線程之間共享的全局變量,例如:

If you have a global variable which is shared between threads, such as:

std::atomic<int> ai;

那么可見(jiàn)性和排序約束取決于您用于操作的內(nèi)存排序參數(shù),以及鎖、線程和訪問(wèn)其他原子變量的同步效果.

then the visibility and ordering constraints depend on the memory ordering parameter you use for operations, and the synchronization effects of locks, threads and accesses to other atomic variables.

在沒(méi)有任何額外同步的情況下,如果一個(gè)線程向 ai 寫(xiě)入一個(gè)值,則無(wú)法保證另一個(gè)線程在任何給定時(shí)間段內(nèi)都能看到該值.該標(biāo)準(zhǔn)規(guī)定它應(yīng)該在合理的時(shí)間段內(nèi)"可見(jiàn),但任何給定的訪問(wèn)都可能返回一個(gè)陳舊的值.

In the absence of any additional synchronization, if one thread writes a value to ai then there is nothing that guarantees that another thread will see the value in any given time period. The standard specifies that it should be visible "in a reasonable period of time", but any given access may return a stale value.

std::memory_order_seq_cst 的默認(rèn)內(nèi)存排序?yàn)樗凶兞康乃?std::memory_order_seq_cst 操作提供了一個(gè)全局總順序.這并不意味著您無(wú)法獲得過(guò)時(shí)的值,但這確實(shí)意味著您獲得的值決定了您的操作在整個(gè)順序中的位置.

The default memory ordering of std::memory_order_seq_cst provides a single global total order for all std::memory_order_seq_cst operations across all variables. This doesn't mean that you can't get stale values, but it does mean that the value you do get determines and is determined by where in this total order your operation lies.

如果您有 2 個(gè)共享變量 xy,初始為零,并且有一個(gè)線程向 x 寫(xiě)入 1,另一個(gè)向 x 寫(xiě)入 2y,那么讀取兩者的第三個(gè)線程可能會(huì)看到 (0,0)、(1,0)、(0,2) 或 (1,2),因?yàn)閮烧咧g沒(méi)有排序約束操作,因此操作可以在全局順序中以任何順序出現(xiàn).

If you have 2 shared variables x and y, initially zero, and have one thread write 1 to x and another write 2 to y, then a third thread that reads both may see either (0,0), (1,0), (0,2) or (1,2) since there is no ordering constraint between the operations, and thus the operations may appear in any order in the global order.

如果兩個(gè)寫(xiě)入都來(lái)自同一個(gè)線程,則 x=1y=2 之前,讀取線程在 y 之前讀取 ycode>x then (0,2) 不再是一個(gè)有效的選項(xiàng),因?yàn)樽x取 y==2 意味著更早的寫(xiě)入 x 是可見(jiàn)的.其他 3 對(duì) (0,0)、(1,0) 和 (1,2) 仍然是可能的,這取決于 2 個(gè)讀取與 2 個(gè)寫(xiě)入的交錯(cuò)方式.

If both writes are from the same thread, which does x=1 before y=2 and the reading thread reads y before x then (0,2) is no longer a valid option, since the read of y==2 implies that the earlier write to x is visible. The other 3 pairings (0,0), (1,0) and (1,2) are still possible, depending how the 2 reads interleave with the 2 writes.

如果您使用其他內(nèi)存排序,例如 std::memory_order_relaxedstd::memory_order_acquire,那么約束會(huì)進(jìn)一步放寬,并且單個(gè)全局排序不再適用.如果沒(méi)有額外的同步,線程甚至不必就兩個(gè)存儲(chǔ)的順序達(dá)成一致以分隔變量.

If you use other memory orderings such as std::memory_order_relaxed or std::memory_order_acquire then the constraints are relaxed even further, and the single global ordering no longer applies. Threads don't even necessarily have to agree on the ordering of two stores to separate variables if there is no additional synchronization.

保證您擁有最新"的唯一方法value 是使用讀-修改-寫(xiě)操作,例如 exchange()compare_exchange_strong()fetch_add().讀-修改-寫(xiě)操作有一個(gè)額外的限制,即它們總是對(duì)最新的"數(shù)據(jù)進(jìn)行操作.值,因此一系列線程的一系列 ai.fetch_add(1) 操作將返回一個(gè)沒(méi)有重復(fù)或間隙的值序列.在沒(méi)有額外約束的情況下,仍然無(wú)法保證哪些線程會(huì)看到哪些值.特別要注意的是,使用 RMW 操作不會(huì)強(qiáng)制其他線程的更改更快地變得可見(jiàn),這只是意味著如果 RMW 沒(méi)有看到這些更改,那么所有線程必須同意它們?cè)谠幼兞康男薷捻樞蛑斜?RMW 操作晚.來(lái)自不同線程的存儲(chǔ)仍然可以延遲任意時(shí)間,這取決于 CPU 實(shí)際何時(shí)將存儲(chǔ)發(fā)布到內(nèi)存(而不僅僅是它自己的存儲(chǔ)緩沖區(qū)),物理執(zhí)行線程的 CPU 相距多遠(yuǎn)(在多處理器系統(tǒng)的情況下),以及緩存一致性協(xié)議的詳細(xì)信息.

The only way to guarantee you have the "latest" value is to use a read-modify-write operation such as exchange(), compare_exchange_strong() or fetch_add(). Read-modify-write operations have an additional constraint that they always operate on the "latest" value, so a sequence of ai.fetch_add(1) operations by a series of threads will return a sequence of values with no duplicates or gaps. In the absence of additional constraints, there's still no guarantee which threads will see which values though. In particular, it is important to note that the use of an RMW operation does not force changes from other threads to become visible any quicker, it just means that if the changes are not seen by the RMW then all threads must agree that they are later in the modification order of that atomic variable than the RMW operation. Stores from different threads can still be delayed by arbitrary amounts of time, depending on when the CPU actually issues the store to memory (rather than just its own store buffer), physically how far apart the CPUs executing the threads are (in the case of a multi-processor system), and the details of the cache coherency protocol.

使用原子操作是一個(gè)復(fù)雜的話題.我建議您閱讀大量背景資料,并在使用原子編寫(xiě)生產(chǎn)代碼之前檢查已發(fā)布的代碼.在大多數(shù)情況下,編寫(xiě)使用鎖的代碼更容易,而且效率不會(huì)明顯降低.

Working with atomic operations is a complex topic. I suggest you read a lot of background material, and examine published code before writing production code with atomics. In most cases it is easier to write code that uses locks, and not noticeably less efficient.

這篇關(guān)于并發(fā):C++11 內(nèi)存模型中的原子性和易失性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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)行排序)
主站蜘蛛池模板: 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 高压互感器,电流互感器,电压互感器-上海鄂互电气科技有限公司 | 蚂蚁分类信息系统 - PHP同城分类信息系统 - MayiCMS | 金库门,金库房,金库门厂家,金库门价格-河北特旺柜业有限公司 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 杭州成人高考_浙江省成人高考网上报名 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 考试试题_试卷及答案_诗词单词成语 - 优易学 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 上海阳光泵业制造有限公司 -【官方网站】 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 伺服电机_直流伺服_交流伺服_DD马达_拓达官方网站 | 临沂招聘网_人才市场_招聘信息_求职招聘找工作请认准【马头商标】 | 杭州|上海贴标机-百科 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 淄博不锈钢无缝管,淄博不锈钢管-鑫门物资有限公司 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | 致胜管家软件服务【在线免费体验】| 潍坊青州古城旅游景点攻略_青州酒店美食推荐-青州旅游网 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 空压机网_《压缩机》杂志 | 出国劳务公司_正规派遣公司[严海] |