Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get a thread object for the main thread, and `join()` with it?

Is there a way to treat the main thread like any other thread with the C++11 (or later) facilities? Concretely, I am looking for is the ability to join() with the main thread. So, basically, I would like to do something like: main_thread.join(), but don't know how to obtain the main_thread object.

The thread constructors do not seem to offer any facilities based for instance on the thread id obtained with get_id(). The this_thread namespace offers also only minimal functionality, but misses for instance join(), which is what I am looking for.

like image 507
smarr Avatar asked Oct 19 '22 19:10

smarr


1 Answers

As pointed out in the comments by @molbdnilo and @yohjb (see also What happens to a detached thread when main() exits?), C++11 semantics say that all threads are ended when the main() function terminates. Since C++11 does not have a pthread_exit() equivalent, the main thread cannot be joined, because the program would end anyway.

So, to answer my question, it does not seem to be possible, and with the terminating semantics of main(), it would not be very useful.

like image 68
2 revs Avatar answered Nov 02 '22 05:11

2 revs