0

A lot of places I see code like this:

void threadFunction()
{
    boost::this_thread::disable_interruption disable;
    while (!boost::this_thread::interruption_requested())
    {
            //do stuff
    }
}

For me this looks like I "disable" the thread function from being interrupted, but then again I test for an interruption. Miraculously, it works. Can someone explain to me what it actually does behind the scenes? Thank you!

Flot2011
  • 4,601
  • 3
  • 44
  • 61
tzippy
  • 6,458
  • 30
  • 82
  • 151

1 Answers1

2

disable_interruption prevents the thread from actually being interrupted; it doesn't prevent the interrupted status being set. Then interruption_requested tests whether the interrupted status has been set.

See the Boost documentation: specifically the "Interruption" section

Various methods are classed as 'interruption points' and will throw an exception if they are called when interruption has been requested, unless interruption has been disabled.

So in short:

  • Disabling interruption prevents interruption from actually stopping the thread, but the interruption still marks the thread as 'interrupt requested'
  • The interruption_requested method checks whether an interrupt has been requested
davmac
  • 20,150
  • 1
  • 40
  • 68