3

I want to interrupt a thread using boost::thread interrupt(). I have the following code which doesn't throw boost::thread_interrupted& exception:

int myClass::myFunction (arg1, arg2) try{
//some code here
    do {   
        boost::this_thread::interruption_point(); 
        //some other code here
    } while (counter != 20000); 
}catch (boost::thread_interrupted&) {
    cout << "interrupted" << endl;
}

If I replace boost::this_thread::interruption_point() with boost::this_thread::sleep( boost::posix_time::milliseconds(150)) exception is throw and interrupt works as it should.

Can someone explain why boost::this_thread::interruption_point() doesn't throw the expected exception?

KoKa
  • 797
  • 1
  • 14
  • 31
  • 1
    its quite possible that with the boost::this_thread::interruption_point() that you have already exited the loop prior to the boost::thread::interrupt() call. boost::this_thread::interruption_point() does not block for any time, its just a check point where the thread can be interrupted and 20000 isn't a very high number of itterations for checking something like this, unless the other code not shown takes a considerable amount of time to complete. – diverscuba23 Sep 29 '14 at 15:53
  • Hi, thanks for the reply. When boost::thread::interrupt() is called I am still inside the loop because I am printing the counter and it hasn't reached 20000. The code after boost::this_thread::interruption_point(); is long and takes time to complete this is why I need to interrupt it in some cases. – KoKa Sep 30 '14 at 08:53
  • I realize now that you cannot put an interruption_point before a function that does not return and expect interrupt to stop it. Once interrupt is called, the next time interruption_point is run, the exception is thrown, correct? – xinthose Apr 02 '15 at 02:37
  • 1
    Function returns, my mistake not include it in the code provided. – KoKa Apr 02 '15 at 07:30

1 Answers1

5

As the commenter noted, there's no way to rule out a simple race condition (depends a lot on your architecture and load on the CPU). The fact adding an explicit sleep "helps" underlines this.

Are you running on a single-core system?

Here's a simple selfcontained example in case you spot something you are doing differently. See this simple tester:

#include <iostream> 
#include <boost/thread.hpp>

struct myClass { 
    int myFunction(int arg1, int arg2);
};

int myClass::myFunction (int arg1, int arg2)
{
    int counter = 0;
    try
    {
        //some code here
        do {   
            boost::this_thread::interruption_point(); 
            //some other code here
            ++counter;
        } while (counter != 20000); 
    } catch (boost::thread_interrupted&) {
        std::cout << "interrupted" << std::endl;
    }
    return counter;
}

void treadf() {
    myClass x;
    std::cout << "Returned: " << x.myFunction(1,2) << "\n";
}

int main()
{
    boost::thread t(treadf);
    //t.interrupt(); // UNCOMMENT THIS LINE
    t.join();
}

It prints

Returned: 20000

Or, if you uncomment the line with t.interrupt()

interrupted
Returned: 0

On my i7 system. See it Live On Coliru

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Hi and thanks for your reply. I am running on a dual core system. I read the example that you provided, but I can't spot any difference between this and my code. – KoKa Sep 30 '14 at 08:55
  • I suppose you could post a SSCCE that shows the problem for you then. Maybe we can spot something – sehe Sep 30 '14 at 08:58