Search code examples
multithreadingboostboost-threadinterruptions

Getting a reference to the current boost::thread/interrupting the main thread


I am trying to get a reference to the main boost::thread of execution, so that I can interrupt it. However, boost::thread doesn't seem to supply any way of doing this.

I have searched on the internet but I can't seem to find an answer to this question.
I did see some old answers, but they seem to be relevant to old boost code.
The closest one said that using boost::thread t; (the default constructor) will give me a reference to the current thread. However, the boost documentation seems to say that that will create a reference to a not-a-thread.

My main goal is to interrupt the main thread, and not get a reference to the current boost::thread, but that seemed like the easiest way.

My current solution is to create a new thread from main, and just join() it. Is there any way to get the current thread object (or interrupt it) and not use this workaround?

Thanks


Solution

  • A default constructed boost::thread object used to (in v1.34 and earlier) refer to the current thread, but it does not do so any longer.

    You cannot interrupt the main thread in boost. Your workaround of starting a new thread to do the work, and joining it from main() is the best way.