简体   繁体   English

std::thread (c++11) 中使用的函数可以有 [[noreturn]] 吗?

[英]Could the function used in std::thread (c++11) have [[noreturn]]?

For std::thread t(foo);对于std::thread t(foo); , does it ever make sense to have a foo [[noreturn]] () {...} ? , 有一个foo [[noreturn]] () {...}有意义吗? For ex.例如。 for a detached thread (Used as a sort of daemon until the apps completion)?对于分离的线程(用作一种守护进程,直到应用程序完成)?

Thread does not make a new process and does not use fork or exec, it makes a thread which should definitely eventually return.线程不创建新进程,也不使用 fork 或 exec,它创建了一个最终肯定会返回的线程。 If your thread function never returns you'll hang on std::thread::join如果您的线程函数永远不会返回,您将挂在std::thread::join

Would it be of any benefit on gcc/clang to mark the function used for the thread with [[noreturn]]?用 [[noreturn]] 标记用于线程的函数对 gcc/clang 有什么好处吗?

Thread functions normally do return.线程函数通常会返回。 So no, they must not be marked with [[noreturn]] .所以不,它们不能用[[noreturn]]标记。

Unless they are coded to have an infinite loop, block forever, or call functions that terminate the thread or the process.除非它们被编码为具有无限循环、永远阻塞或调用终止线程或进程的函数。 In that case you might like to mark them as noreturn .在这种情况下,您可能希望将它们标记为noreturn

Note, that if you are using low-level functions like pthread_create to create a thread or other function which definition is not available at compile time, noreturn attribute will not have any effect on the code that invokes your function through a pointer.请注意,如果您使用像pthread_create这样的低级函数来创建一个线程或其他定义在编译时不可用的函数,则noreturn属性不会对通过指针调用您的函数的代码产生任何影响。

The return value of a thread function of a detached thread is ignored.忽略分离线程的线程函数的返回值。

I expect it (std::thread construction) to be some kind of fork + execve variation on my system and once successful it would have nothing to return to.我希望它(std::thread 构造)是我系统上的某种 fork + execve 变体,一旦成功,它就没有什么可返回的了。

std::thread creates a separate thread (within the same process). std::thread 创建一个单独的线程(在同一进程内)。 fork + execve work on multiple processes. fork + execve 在多个进程上工作。

Would it be of any benefit on gcc/clang to mark the function used for the thread with [[noreturn]]?用 [[noreturn]] 标记用于线程的函数对 gcc/clang 有什么好处吗?

Only if the function you pass to std::thread does not return (that is, if the function calls std::exit, std::terminate, throws in all cases or starts an infinite loop, and so on).仅当您传递给 std::thread 的函数不返回时(即,如果该函数调用 std::exit、std::terminate、在所有情况下抛出或开始无限循环等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM