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)行排序)
主站蜘蛛池模板: 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 盘古网络技术有限公司| 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 无缝方管|无缝矩形管|无缝方矩管|无锡方管厂家 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 | 武汉画册印刷厂家-企业画册印刷-画册设计印刷制作-宣传画册印刷公司 - 武汉泽雅印刷厂 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 打包钢带,铁皮打包带,烤蓝打包带-高密市金和金属制品厂 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 | 安徽泰科检测科技有限公司【官方网站】 | 防爆暖风机_防爆电暖器_防爆电暖风机_防爆电热油汀_南阳市中通智能科技集团有限公司 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 【连江县榕彩涂料有限公司】官方网站 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 实战IT培训机构_IT培训班选大学生IT技术培训中心_中公优就业 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 | ORP控制器_ORP电极价格-上优泰百科 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 西门子代理商_西门子变频器总代理-翰粤百科| 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 烟台条码打印机_烟台条码扫描器_烟台碳带_烟台数据采集终端_烟台斑马打印机-金鹏电子-金鹏电子 | 破碎机锤头_耐磨锤头_合金锤头-鼎成机械一站式耐磨铸件定制服务 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | TPU薄膜_TPU薄膜生产厂家_TPU热熔胶膜厂家定制_鑫亘环保科技(深圳)有限公司 | 实战IT培训机构_IT培训班选大学生IT技术培训中心_中公优就业 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 |