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

當(dāng) main() 退出時(shí),分離的線程會(huì)發(fā)生什么?

What happens to a detached thread when main() exits?(當(dāng) main() 退出時(shí),分離的線程會(huì)發(fā)生什么?)
本文介紹了當(dāng) main() 退出時(shí),分離的線程會(huì)發(fā)生什么?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

假設(shè)我正在啟動(dòng)一個(gè) std::thread 然后 detach() 它,所以即使 std::thread 曾經(jīng)代表它,超出了范圍.

Assume I'm starting a std::thread and then detach() it, so the thread continues executing even though the std::thread that once represented it, goes out of scope.

進(jìn)一步假設(shè)程序沒有可靠的加入分離線程的協(xié)議1,所以當(dāng)main()退出時(shí)分離線程仍然運(yùn)行.

Assume further that the program does not have a reliable protocol for joining the detached thread1, so the detached thread still runs when main() exits.

我在標(biāo)準(zhǔn)中找不到任何內(nèi)容(更準(zhǔn)確地說(shuō),在 N3797 C++14 草案中),它描述了應(yīng)該發(fā)生的情況,1.10 和 30.3 都沒有包含相關(guān)的措辭.

I cannot find anything in the standard (more precisely, in the N3797 C++14 draft), which describes what should happen, neither 1.10 nor 30.3 contain pertinent wording.

1 另一個(gè)可能等效的問(wèn)題是:分離的線程是否可以再次加入",因?yàn)闊o(wú)論您發(fā)明要加入的協(xié)議,信令部分都必須在線程仍在運(yùn)行,操作系統(tǒng)調(diào)度程序可能會(huì)在信號(hào)剛執(zhí)行完后決定讓線程休眠一個(gè)小時(shí),而接收端無(wú)法可靠地檢測(cè)到線程實(shí)際上已完成.

1 Another, probably equivalent, question is: "can a detached thread ever be joined again", because whatever protocol you're inventing to join, the signalling part would have to be done while the thread was still running, and the OS scheduler might decide to put the thread to sleep for an hour just after signalling was performed with no way for the receiving end to reliably detect that the thread actually finished.

如果在分離線程運(yùn)行時(shí)用完 main() 是未定義的行為,那么 any 使用 std::thread::detach() 是未定義的行為,除非主線程永遠(yuǎn)不會(huì)退出2.

If running out of main() with detached threads running is undefined behaviour, then any use of std::thread::detach() is undefined behaviour unless the main thread never exits2.

因此,在運(yùn)行分離線程的情況下用完 main() 必須具有定義效果.問(wèn)題是:哪里(在 C++ 標(biāo)準(zhǔn)中,不是 POSIX,不是 OS 文檔,...)是那些定義的效果.

Thus, running out of main() with detached threads running must have defined effects. The question is: where (in the C++ standard, not POSIX, not OS docs, ...) are those effects defined.

2 一個(gè)分離的線程不能被加入(在std::thread::join()的意義上).您可以等待來(lái)自分離線程的結(jié)果(例如通過(guò)來(lái)自std::packaged_task的未來(lái),或者通過(guò)計(jì)數(shù)信號(hào)量或標(biāo)志和條件變量),但這不會(huì)不保證線程已經(jīng)完成執(zhí)行.實(shí)際上,除非您將信號(hào)部分放入線程的第一個(gè)自動(dòng)對(duì)象的析構(gòu)函數(shù)中,否則通常是在信號(hào)之后運(yùn)行的代碼(析構(gòu)函數(shù))代碼.如果操作系統(tǒng)安排主線程在分離的線程運(yùn)行完上述析構(gòu)函數(shù)之前使用結(jié)果并退出,那么 ^Wi 定義會(huì)發(fā)生什么?

2 A detached thread cannot be joined (in the sense of std::thread::join()). You can wait for results from detached threads (e.g. via a future from std::packaged_task, or by a counting semaphore or a flag and a condition variable), but that doesn't guarantee that the thread has finished executing. Indeed, unless you put the signalling part into the destructor of the first automatic object of the thread, there will, in general, be code (destructors) that run after the signalling code. If the OS schedules the main thread to consume the result and exit before the detached thread finishes running said destructors, what will^Wis defined to happen?

推薦答案

原問(wèn)題main() 退出時(shí)分離的線程會(huì)發(fā)生什么"的答案是:

The answer to the original question "what happens to a detached thread when main() exits" is:

它會(huì)繼續(xù)運(yùn)行(因?yàn)闃?biāo)準(zhǔn)沒有說(shuō)它已停止),而且這是明確定義的,只要它既不涉及其他線程的 (automatic|thread_local) 變量也不涉及靜態(tài)對(duì)象.

It continues running (because the standard doesn't say it is stopped), and that's well-defined, as long as it touches neither (automatic|thread_local) variables of other threads nor static objects.

這似乎允許線程管理器作為靜態(tài)對(duì)象([basic.start.term]/4 中的注釋說(shuō)明了這一點(diǎn),感謝@dyp 提供了指針).

This appears to be allowed to allow thread managers as static objects (note in [basic.start.term]/4 says as much, thanks to @dyp for the pointer).

當(dāng)靜態(tài)對(duì)象的銷毀完成時(shí)會(huì)出現(xiàn)問(wèn)題,因?yàn)槿缓髨?zhí)行進(jìn)入一個(gè)只能執(zhí)行信號(hào)處理程序中允許的代碼的狀態(tài)([basic.start.term]/1, 1st sentence).在 C++ 標(biāo)準(zhǔn)庫(kù)中,只有 庫(kù)([support.runtime]/9, 2nd sentence).特別是,一般來(lái)說(shuō),排除 condition_variable(它是實(shí)現(xiàn)定義的,是否保存以在信號(hào)處理程序中使用,因?yàn)樗皇?<原子>).

Problems arise when the destruction of static objects has finished, because then execution enters a regime where only code allowed in signal handlers may execute ([basic.start.term]/1, 1st sentence). Of the C++ standard library, that is only the <atomic> library ([support.runtime]/9, 2nd sentence). In particular, that—in general—excludes condition_variable (it's implementation-defined whether that is save to use in a signal handler, because it's not part of <atomic>).

除非此時(shí)您已經(jīng)解開堆棧,否則很難看出如何避免未定義的行為.

Unless you've unwound your stack at this point, it's hard to see how to avoid undefined behaviour.

第二個(gè)問(wèn)題分離的線程是否可以再次加入"的答案是:

The answer to the second question "can detached threads ever be joined again" is:

是的,使用 *_at_thread_exit 系列函數(shù)(notify_all_at_thread_exit()、std::promise::set_value_at_thread_exit()、...).

Yes, with the *_at_thread_exit family of functions (notify_all_at_thread_exit(), std::promise::set_value_at_thread_exit(), ...).

正如問(wèn)題的腳注 [2] 中所指出的,向條件變量或信號(hào)量或原子計(jì)數(shù)器發(fā)出信號(hào)不足以加入分離的線程(在確保其執(zhí)行結(jié)束的意義上有-發(fā)生在等待線程接收到所述信號(hào)之前),因?yàn)橐话銇?lái)說(shuō),在例如之后會(huì)執(zhí)行更多的代碼條件變量的notify_all(),特別是自動(dòng)和線程局部對(duì)象的析構(gòu)函數(shù).

As noted in footnote [2] of the question, signalling a condition variable or a semaphore or an atomic counter is not sufficient to join a detached thread (in the sense of ensuring that the end of its execution has-happened-before the receiving of said signalling by a waiting thread), because, in general, there will be more code executed after e.g. a notify_all() of a condition variable, in particular the destructors of automatic and thread-local objects.

在線程做的最后一件事(自動(dòng)和線程局部對(duì)象的析構(gòu)函數(shù)已經(jīng)發(fā)生之后)運(yùn)行信號(hào)是_at_thread_exitcode> 函數(shù)系列的設(shè)計(jì)目標(biāo).

Running the signalling as the last thing the thread does (after destructors of automatic and thread-local objects has-happened) is what the _at_thread_exit family of functions was designed for.

因此,為了避免在沒有超出標(biāo)準(zhǔn)要求的任何實(shí)現(xiàn)保證的情況下出現(xiàn)未定義的行為,您需要(手動(dòng))將分離的線程與 _at_thread_exit 函數(shù)一起執(zhí)行信號(hào)使分離的線程執(zhí)行對(duì)信號(hào)處理程序也是安全的代碼.

So, in order to avoid undefined behaviour in the absence of any implementation guarantees above what the standard requires, you need to (manually) join a detached thread with an _at_thread_exit function doing the signalling or make the detached thread execute only code that would be safe for a signal handler, too.

這篇關(guān)于當(dāng) main() 退出時(shí),分離的線程會(huì)發(fā)生什么?的文章就介紹到這了,希望我們推薦的答案對(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)行排序)
主站蜘蛛池模板: 电子天平-华志电子天平厂家 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 影合社-影视人的内容合作平台 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 快速门厂家-快速卷帘门-工业快速门-硬质快速门-西朗门业 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 披萨石_披萨盘_电器家电隔热绵加工定制_佛山市南海区西樵南方综合保温材料厂 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 啤酒设备-小型啤酒设备-啤酒厂设备-济南中酿机械设备有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 苏州教学设备-化工教学设备-环境工程教学模型|同科教仪 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 出国劳务公司_正规派遣公司[严海] | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 |