3

As per How can I tell reliably if a boost thread has exited its run method?, thankfully you can join a finished thread and avoid the race condition that arises if you had to conditionally join a thread only if running.

But what about thread::interrupt()? Can it be called after the thread exited?

Community
  • 1
  • 1
haelix
  • 4,245
  • 4
  • 34
  • 56

1 Answers1

3

Yes, it is safe to call this method.

From the documentation:

If *this refers to a thread of execution, request that the thread will be interrupted the next time it enters one of the predefined interruption points with interruption enabled, or if it is currently blocked in a call to one of the predefined interruption points with interruption enabled.

When the boost::thread object (*this) is not "a thread of execution", that means is not running, calling this method does nothing.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • Great to know. Does it follow, that the thread can safely interrupt itself (having access to the thread object)? Causing just to exit itself at next interruption point / sleep? I think so. – Tomasz Gandor Nov 21 '14 at 12:46
  • I am not 100% convinced, because the docs only state what happens if the thread is still running but I couldnt find any explicit mention of what happens if the thread is not running. On the other hand the only reasonable thing to do when the thread is not running is nothing... – 463035818_is_not_an_ai Aug 30 '17 at 14:05