Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Thread - How to acknowledge interrupt

I have blocking task which will be performed by find_the_question() function. However, I do not want thread executing this function take more than 10 seconds. So in case it takes more than 10 seconds, I want to close that thread with cleaning all the resources.

I tried to write a code for that, but somehow I am not able to get a interrupt in find_the_question() function if thread takes more than 10 seconds. Could you please tell me what am I doing wrong?

void find_the_question(std::string value)
{
    //allocate x resources
    try{
        //do some process on resources
            sleep(14);
        //clean resources
    }
    catch(boost::thread_interrupted const& )
    {
        //clean resources
        std::cout << "Worker thread interrupted" << std::endl;
    }
}

int main()
{
        boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
        std::cout << "In main" << std::endl;
        boost::thread t1(find_the_question, "Can you block me");

        t1.interrupt();
        if (t1.timed_join(timeout))
        {
          //finished
          std::cout << "Worker thread finished" << std::endl;
        }
        else
        {
          //Not finished;
          std::cout << "Worker thread not finished" << std::endl;
        }

        std::cout << "In main end" << std::endl;
}

Output: If t1 takes more than 10 seconds to complete, I am getting following console output.

std::cout << "In main" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;

whereas, I am expecting following output

std::cout << "In main" << std::endl;
std::cout << "Worker thread interrupted" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;

Could you please tell me what am I doing wrong.

Thanks in advance

like image 977
Amey Jah Avatar asked Sep 06 '11 07:09

Amey Jah


1 Answers

For using boost::thread::interrupt(), you have to use boost::thread::sleep() for it to work.

A running thread can be interrupted by invoking the interrupt() member function of the corresponding boost::thread object. When the interrupted thread next executes one of the specified interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost::thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate. As with any other exception, the stack will be unwound, and destructors for objects of automatic storage duration will be executed

Predefined interruption points:

The following functions are interruption points, which will throw boost::thread_interrupted if interruption is enabled for the current thread, and interruption is requested for the current thread:

boost::thread::join()
boost::thread::timed_join()
boost::condition_variable::wait()
boost::condition_variable::timed_wait()
boost::condition_variable_any::wait()
boost::condition_variable_any::timed_wait()
boost::thread::sleep()
boost::this_thread::sleep()
boost::this_thread::interruption_point()
like image 115
Tony The Lion Avatar answered Sep 28 '22 17:09

Tony The Lion