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

當 main() 退出時,分離的線程會發生什么?

What happens to a detached thread when main() exits?(當 main() 退出時,分離的線程會發生什么?)
本文介紹了當 main() 退出時,分離的線程會發生什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

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

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.

進一步假設程序沒有可靠的加入分離線程的協議1,所以當main()退出時分離線程仍然運行.

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.

我在標準中找不到任何內容(更準確地說,在 N3797 C++14 草案中),它描述了應該發生的情況,1.10 和 30.3 都沒有包含相關的措辭.

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 另一個可能等效的問題是:分離的線程是否可以再次加入",因為無論您發明要加入的協議,信令部分都必須在線程仍在運行,操作系統調度程序可能會在信號剛執行完后決定讓線程休眠一個小時,而接收端無法可靠地檢測到線程實際上已完成.

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.

如果在分離線程運行時用完 main() 是未定義的行為,那么 any 使用 std::thread::detach() 是未定義的行為,除非主線程永遠不會退出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.

因此,在運行分離線程的情況下用完 main() 必須具有定義效果.問題是:哪里(在 C++ 標準中,不是 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 一個分離的線程不能被加入(在std::thread::join()的意義上).您可以等待來自分離線程的結果(例如通過來自std::packaged_task的未來,或者通過計數信號量或標志和條件變量),但這不會不保證線程已經完成執行.實際上,除非您將信號部分放入線程的第一個自動對象的析構函數中,否則通常是在信號之后運行的代碼(析構函數)代碼.如果操作系統安排主線程在分離的線程運行完上述析構函數之前使用結果并退出,那么 ^Wi 定義會發生什么?

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?

推薦答案

原問題main() 退出時分離的線程會發生什么"的答案是:

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

它會繼續運行(因為標準沒有說它已停止),而且這是明確定義的,只要它既不涉及其他線程的 (automatic|thread_local) 變量也不涉及靜態對象.

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.

這似乎允許線程管理器作為靜態對象([basic.start.term]/4 中的注釋說明了這一點,感謝@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).

當靜態對象的銷毀完成時會出現問題,因為然后執行進入一個只能執行信號處理程序中允許的代碼的狀態([basic.start.term]/1, 1st sentence).在 C++ 標準庫中,只有 庫([support.runtime]/9, 2nd sentence).特別是,一般來說,排除 condition_variable(它是實現定義的,是否保存以在信號處理程序中使用,因為它不是 <原子>).

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>).

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

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

第二個問題分離的線程是否可以再次加入"的答案是:

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

是的,使用 *_at_thread_exit 系列函數(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(), ...).

正如問題的腳注 [2] 中所指出的,向條件變量或信號量或原子計數器發出信號不足以加入分離的線程(在確保其執行結束的意義上有-發生在等待線程接收到所述信號之前),因為一般來說,在例如之后會執行更多的代碼條件變量的notify_all(),特別是自動和線程局部對象的析構函數.

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.

在線程做的最后一件事(自動和線程局部對象的析構函數已經發生之后)運行信號是_at_thread_exitcode> 函數系列的設計目標.

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.

因此,為了避免在沒有超出標準要求的任何實現保證的情況下出現未定義的行為,您需要(手動)將分離的線程與 _at_thread_exit 函數一起執行信號使分離的線程執行對信號處理程序也是安全的代碼.

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.

這篇關于當 main() 退出時,分離的線程會發生什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 排序功能對列表進行排序)
主站蜘蛛池模板: 亚洲精品久 | 国产成人av在线播放 | 久久婷婷麻豆国产91天堂 | 国产色婷婷久久99精品91 | 国产精品久久久久久久久久久久午夜片 | 91精品国产一区二区在线观看 | 精品视频国产 | www国产亚洲精品久久网站 | 国产精品久久久久久久久久久久 | 日韩精品久久久久 | 中文字幕亚洲欧美 | 伊人影院在线观看 | 日韩欧美综合在线视频 | 色.com| 亚洲天堂二区 | 国产精品海角社区在线观看 | 亚洲va中文字幕 | 日韩视频成人 | 日日干干| 一区在线播放 | 国产精品久久久久久亚洲调教 | 欧美在线一区二区三区 | 五月婷亚洲| 国产农村一级国产农村 | 国产 欧美 日韩 一区 | 看片地址 | 国产精品久久久久久久久久久久久 | 精品久久久久久久久久久久久久 | 精品产国自在拍 | 日韩天堂av | 日韩精品免费在线观看 | 自拍亚洲| 国产精品美女一区二区 | 国产免费一区二区 | 污视频在线免费观看 | 国产精品欧美一区喷水 | 成人精品一区二区三区 | 中文字幕视频在线看5 | 亚洲av毛片| 美国一级片在线观看 | 午夜专区 |