gpt4 book ai didi

c++ - C++11 线程是否为分离线程提供了一种在主线程退出后继续运行的方法?

转载 作者:太空狗 更新时间:2023-10-29 23:13:34 26 4
gpt4 key购买 nike

通常,当 main() 退出时,所有线程都会被杀死。 pthread_exit(3)

To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).

是否有等效的 C++11 API 调用?像 std::this_thread::exit(0) 这样的东西?

最佳答案

Working Draft, Standard for Programming Language C++ 的第 1121 页从 2012-01-16 开始似乎声明一旦主线程退出,它的分离线程也将被清理(除非我误解它):

void detach();

Requires: joinable() is true.

Effects: The thread represented by *this continues execution without the calling thread blocking. When detach() returns, *this no longer represents the possibly continuing thread of execution. When the thread previously represented by *this ends execution, the implementation shall release any owned resources.

Postcondition: get_id() == id().

Throws: system_error when an exception is required (30.2.2).

Error conditions:

— no_such_process — if the thread is not valid.

— invalid_argument — if the thread is not joinable.

关于c++ - C++11 线程是否为分离线程提供了一种在主线程退出后继续运行的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38107707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com