5

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.

smarr
  • 763
  • 1
  • 6
  • 17
  • Usually you start a thread from the main thread and then join() the thread you started.. not the other way around. – KoKuToru Jan 05 '15 at 15:18
  • [std::thread::join](http://en.cppreference.com/w/cpp/thread/thread/join) method is there for c++11.Whats problem? – Ankur Jan 05 '15 at 15:19
  • Er...if you want to join the main thread, what exactly is the difference to just ending the current thread? It's not like you're ever going to continue; the program is over when the main thread ends. – Wintermute Jan 05 '15 at 15:19
  • I need the ability to do `main_thread.join()`. In my case, the main thread is just like any other thread, not specially at all. Each thread is running arbitrary user code, which needs to be able to call `join()`. – smarr Jan 05 '15 at 15:21
  • 10
    When `main` exits, all other threads are destroyed. The main thread is by definition *not* "just like any other thread". – molbdnilo Jan 05 '15 at 15:23
  • You *could* spawn your own "main thread" which *is* just like any other thread and join that, of course. – molbdnilo Jan 05 '15 at 15:26
  • NO. see also http://stackoverflow.com/questions/19744250/c11-what-happens-to-a-detached-thread-when-main-exits – yohjp Jan 05 '15 at 15:27
  • Thanks molbdnilo, wasn't aware that main mandates to stop execution of all threads. Ok, I guess, that's the answer I was looking for. – smarr Jan 05 '15 at 15:28
  • @molbdnilo You can end the main thread without terminating all the others ([link](http://stackoverflow.com/questions/3559463/is-it-ok-to-call-pthread-exit-from-main)) – ElderBug Jan 05 '15 at 15:32
  • @ElderBug ok, well, I guess that's outside the C++11 language, and since I do not say any equivalent for `thread`, it looks like I'll have to work around it in another way. – smarr Jan 05 '15 at 15:36
  • Who closevoted this as requiring a testcase? Silly. – Lightness Races in Orbit Jan 06 '15 at 13:20

1 Answers1

3

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.

Community
  • 1
  • 1
smarr
  • 763
  • 1
  • 6
  • 17