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

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

对于 std::thread t(foo);,有一个 foo [[noreturn]] () {...} 有意义吗?对于前。对于分离线程(在应用程序完成之前用作一种守护进程)?

Thread 不会创建新进程,也不使用 fork 或 exec,它会创建一个最终肯定应该 return 的线程。如果你的线程函数从不 returns 你会挂在 std::thread::join

Would it be of any benefit on gcc/clang to mark the function used for the thread with [[noreturn]]?

线程函数正常 return。所以不,它们不能用 [[noreturn]] 标记。

除非它们被编码为具有无限循环、永远阻塞或调用终止线程或进程的函数。在这种情况下,您可能希望将它们标记为 noreturn

请注意,如果您使用像 pthread_create 这样的低级函数来创建线程或其他在编译时定义不可用的函数,noreturn 属性将不会对通过指针调用函数的代码。

分离线程的线程函数的 return 值被忽略。

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 在多个进程上工作。

Would it be of any benefit on gcc/clang to mark the function used for the thread with [[noreturn]]?

只有当你传递给std::thread的函数没有return时(也就是说,如果函数调用std::exit,std::terminate,在所有情况下都会抛出或启动一个无限循环,等等)。