Boost Thread-如何确认中断 [英] Boost Thread - How to acknowledge interrupt

查看:235
本文介绍了Boost Thread-如何确认中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将由find_the_question()函数执行的阻止任务.但是,我不希望执行此功能的线程花费超过10秒的时间.因此,如果要花费10秒钟以上的时间,我想通过清理所有资源来关闭该线程.

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.

我试图为此编写代码,但是如果线程花费的时间超过10秒,我就无法在find_the_question()函数中获得中断.你能告诉我我在做什么错吗?

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;
}

输出: 如果t1花费了10秒钟以上才能完成,那么我将获得以下控制台输出.

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;

而我期望以下输出

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.

预先感谢

推荐答案

要使用boost :: thread :: interrupt(),必须使用boost :: thread :: sleep()才能正常工作.

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

可以通过调用interrupt()成员来中断正在运行的线程 相应的boost :: thread对象的函数.当...的时候 被中断的线程接下来执行指定的中断之一 点(或在执行一个点时当前被阻止) 启用中断,然后boost :: thread_interrupted异常将 被抛出到中断的线程中.如果没有抓住,这将导致 被中断线程的执行终止.与任何 其他例外,将取消堆栈堆栈,并使用析构函数 具有自动存储期限的对象将被执行

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

预定义的中断点:

以下功能是中断点,它们会抛出 如果当前启用了中断,则boost :: thread_interrupted 线程,并请求中断当前线程:

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()

这篇关于Boost Thread-如何确认中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆