c++c++11function-attributes

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


For std::thread t(foo);, does it ever make sense to have a foo [[noreturn]] () {...}? For ex. for a detached thread (Used as a sort of daemon until the apps completion)?


Solution

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

    Thread functions normally do return. So no, they must not be marked with [[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.

    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.

    The return value of a thread function of a detached thread is ignored.