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

為什么從`std::async` 阻塞返回未來的析構函數?

Why is the destructor of a future returned from `std::async` blocking?(為什么從`std::async` 阻塞返回未來的析構函數?)
本文介紹了為什么從`std::async` 阻塞返回未來的析構函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當試圖回答另一個 Stackoverflow 問題,我意識到這個簡單的 C++11 代碼段隱式地阻塞了調用線程:

std::async(std::launch::async, run_async_task)

對我來說,這似乎是規范的 C++11 異步啟動任務而不關心結果的方式.相反,為了實現這一點,必須明顯地創建和分離一個線程(參見 answer 提到的問題).>

所以這是我的問題:std::future 的析構函數是否有任何關于安全/正確性的原因必須阻塞?如果它只在 get 上阻塞還不夠,否則,如果我對返回值或異常不感興趣,它只是觸發并忘記?

解決方案

阻止 std::async 和線程返回的期貨的析構函數:這是一個有爭議的話題.以下按時間順序排列的論文列表反映了委員會成員的一些討論:

  • N2802:請求重新考慮線程的 detach-on-destruction對象 作者:漢斯·博姆
  • N3630:異步、~future 和~線程(修訂版 1) 作者:赫伯·薩特
  • N3636:~thread 應該加入 作者 Herb Sutter
  • N3637:異步和 ~future(修訂版 3) 作者:赫伯·薩特、錢德勒·卡魯斯、尼克拉斯·古斯塔夫森
  • N3679:Async() 未來的析構函數必須等待作者:漢斯·博姆
  • N3773:異步和 ~future(修訂版 4) 作者:赫伯·薩特、錢德勒·卡魯斯、尼克拉斯·古斯塔夫森
  • N3776:未來的措辭 作者 Herb Sutter
  • N3777:棄用異步的措辭 作者 Herb Sutter

盡管有很多討論,但 C++14 沒有計劃改變 std::futurestd::thread.

關于你的問題,最有趣的論文可能是 Hans Boehm 的第二篇.我引用了一些部分來回答你的問題.

<塊引用>

N3679:Async() 未來的析構函數必須等待

[..] 由 async() 返回的具有 async 啟動策略的 Futures 在它們的析構函數中等待相關的共享狀態準備就緒.這可以防止出現關聯線程繼續運行的情況,并且不再有等待它完成的方法,因為關聯的未來已被銷毀.如果沒有英勇的努力以其他方式等待完成,這樣一個失控"的線程可以繼續運行超過它所依賴的對象的生命周期.

[例子]

最終結果很可能是跨線程內存粉碎".如果 get()wait() 在它們 [期貨] 被銷毀之前被調用 [..],這個問題當然可以避免.困難 [..] 是意外的異常可能會導致該代碼被繞過.因此,通常需要某種范圍保護裝置來確保安全.如果程序員忘記添加范圍保護,攻擊者可能會生成例如在適當的時候發生 bad_alloc 異常以利用疏忽,并導致堆棧被覆蓋.還可以控制用于覆蓋堆棧的數據,從而獲得對過程的控制.這是一個非常微妙的錯誤,根據我們的經驗,它很可能在實際代碼中被忽略.

更新:Michael Wong 的旅行報告還包含一些關于 2013 年 9 月會議成果的有趣信息:

<塊引用>

關于異步析構函數不應阻塞的問題,我們對此進行了大量討論.[..] 唯一獲得大量支持的立場是 [..] 給出警告,即未來的析構函數不會阻塞,除非從 async 返回,使其成為顯著的例外.[..] 經過大量討論,我們嘗試攜帶的唯一部分是 N3776,試圖澄清 ~future~shared_future 不阻塞的立場,除了可能存在異步.有一次嘗試沿 C 的路線發出棄用. 棄用異步而不替換.這個議案實際上幾乎是提出來的.但是 [..] 它甚至在到達手術臺之前就已經死亡.

When trying to answer another Stackoverflow question, I realized that this simple C++11 snippet is implicitly blocking the calling thread:

std::async(std::launch::async, run_async_task)

To me this would have seemed the canonical C++11 way to launch a task asynchronously without caring about the result. Instead one has to apparently explicitly create and detach a thread (see answer to mentioned question) in order to achieve this.

So here's my question: Is there any reason in regards to safety/correctness that the destructor of a std::future has to be blocking? Wouldn't it be enough if it blocks on get only and otherwise, if I'm not interested in the return value or exception, it's simply fire and forget?

解決方案

Blocking destructors of futures returned by std::async and of threads: That's a controversial topic. The following list of papers in chronological order reflects some of the discussions by the members of the committee:

  • N2802: A plea to reconsider detach-on-destruction for thread objects by Hans Boehm
  • N3630: async, ~future, and ~thread (Revision 1) by Herb Sutter
  • N3636: ~thread Should Join by Herb Sutter
  • N3637: async and ~future (Revision 3) by Herb Sutter, Chandler Carruth, Niklas Gustafsson
  • N3679: Async() future destructors must wait by Hans Boehm
  • N3773: async and ~future (Revision 4) by Herb Sutter, Chandler Carruth, Niklas Gustafsson
  • N3776: Wording for ~future by Herb Sutter
  • N3777: Wording for deprecating async by Herb Sutter

Although there was a lot of discussion, there are no changes planned for C++14 regarding the blocking behaviour of the destructors of std::future and std::thread.

Regarding your question, the most interesting paper is probably the second by Hans Boehm. I quote some parts to answer your question.

N3679: Async() future destructors must wait

[..] Futures returned by async() with async launch policy wait in their destructor for the associated shared state to become ready. This prevents a situation in which the associated thread continues to run, and there is no longer a means to wait for it to complete because the associated future has been destroyed. Without heroic efforts to otherwise wait for completion, such a "run-away" thread can continue to run past the lifetime of the objects on which it depends.

[Example]

The end result is likely to be a cross-thread "memory smash". This problem is of course avoided if get() or wait() is called [..] before they [the futures] are destroyed. The difficulty [..] is that an unexpected exception may cause that code to be bypassed. Thus some sort of scope guard is usually needed to ensure safety. If the programmer forgets to add the scope guard, it appears likely that an attacker could generate e.g. a bad_alloc exception at an opportune point to take advantage of the oversight, and cause a stack to be overwritten. It may be possible to also control the data used to overwrite the stack, and thus gain control over the process. This is a sufficiently subtle error that, in our experience, it is likely to be overlooked in real code.

Update: Michael Wong's Trip Report also contains some interesting information regarding the outcomes of the meeting in September 2013:

The View from the C++ Standard meeting September 2013 Part 2 of 2.

On the issue that async destructors should not block we devoted a great deal of discussion on it. [..] The only position that received considerable support was [..] giving advisory that future destructors will not block, unless returned from async, making it the notable exception. [..] After significant discussion, the only part that we tried to carry was N3776, an attempt to clarify the position that ~future and ~shared_future don’t block except possibly in the presence of async. There was an attempt to issue a deprecation along the lines of C. Deprecate async without replacement. This motion was actually almost put forward. But [..] it died even before it reached the operating table.

這篇關于為什么從`std::async` 阻塞返回未來的析構函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 创客匠人-让IP变现不走弯路 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 荣事达手推洗地机_洗地机厂家_驾驶式扫地机_工业清洁设备 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 洗瓶机厂家-酒瓶玻璃瓶冲瓶机-瓶子烘干机-封口旋盖压盖打塞机_青州惠联灌装机械 | 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 西宁装修_西宁装修公司-西宁业之峰装饰-青海业之峰墅级装饰设计公司【官网】 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 通风天窗,通风气楼,屋顶通风天窗,屋顶通风天窗公司 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 电动车头盔厂家_赠品头盔_安全帽批发_山东摩托车头盔—临沂承福头盔 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 华中线缆有限公司-电缆厂|电缆厂家|电线电缆厂家 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 天津仓储物流-天津电商云仓-天津云仓一件代发-博程云仓官网 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 | 广西资质代办_建筑资质代办_南宁资质代办理_新办、增项、升级-正明集团 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 同学聚会纪念册制作_毕业相册制作-成都顺时针宣传画册设计公司 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 微妙网,专业的动画师、特效师、CG模型设计师网站! - wmiao.com 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 |