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

- 523
- 1
- 4
- 11
-
2Threads and process are two different things? and your question is too vague. – Misgevolution Sep 24 '15 at 13:28
-
Your expectation is wrong, therefore the rest of the question is actually moot. – anderas Sep 24 '15 at 13:29
-
@Misgevolution, yes of course, no execve... It should be clone + what? Jump to the new function and expect noreturn from it? – Guest 86 Sep 24 '15 at 13:37
-
@anderas, Yes you're right but the way you warded it wasn't really helpful and without Misgevolutions comment, I didn't understand it. – Guest 86 Sep 24 '15 at 13:38
3 Answers
Thread does not make a new process and does not use fork or exec, it makes a thread which should definitely eventually return. If your thread function never returns you'll hang on std::thread::join

- 1,106
- 2
- 12
- 28
-
Thank you for the answer, I fixed my misconception and rephrased the question (I'm not interested in a join-able thread) – Guest 86 Sep 24 '15 at 13:50
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.

- 131,725
- 17
- 180
- 271
-
Do they return even if they are detached? It's not very clear to me, what they should return to? – Guest 86 Sep 24 '15 at 13:51
-
1@Guest86 They do. The return value of the thread function of a detached thread is ignored. – Maxim Egorushkin Sep 24 '15 at 14:01
-
1This answer is incorrect. `[[noreturn]]` is a valid attribute on a thread function that does not return, and a compiler can use that to optimize out the code in the `std::thread` implementation that happens *after* the thread is dispatched. Doing so is a strange thing to do, and you probably will have issues at application shutdown. – Yakk - Adam Nevraumont Sep 24 '15 at 14:51
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 creates a separate thread (within the same process). fork + execve work on multiple processes.
Would it be of any benefit on gcc/clang to mark the function used for the thread with [[noreturn]]?
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).

- 26,809
- 3
- 46
- 82
-
I see, My case is a thread running as a mini "daemon" for the duration of the program, I'll have to clarify what happens with this kind of thread when the main thread is closed (If the OS cleans it up)... Thnaks! – Guest 86 Sep 24 '15 at 13:41