简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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