升压线程在中断时不打印退出消息 [英] boost thread on interruption doesn't print exit message

查看:191
本文介绍了升压线程在中断时不打印退出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码用于执行三个线程,其中第二个线程应该在按下enter并打印退出消息时中断:

I have this piece of code for executing three threads where the second thread should get interrupted on pressing enter and print the exit message:

void input_val()
{
    // DO STUFF
return;
}

void process_val()
{
       // DO STUFF
       try{
        cout << "waiting for ENTER..." << endl;
        boost::this_thread::sleep(boost::posix_time::milliseconds(200));
    }
    catch(boost::thread_interrupted&){
        cout << "exit process thread" << endl;
        return;
    }
    return;
}


void output_val()
{
    // DO STUFF
}

int main()
{
    char key_pressed;

    boost::thread input_thread(boost::bind(&input_val));
    boost::thread process_thread(boost::bind(&process_val));
    boost::thread output_thread(boost::bind(&output_val));

    cin.get(key_pressed);
    process_thread.interrupt();

    input_thread.join();
    process_thread.join();
    output_thread.join();
    return 0;
}

process_thread在ENTER时被中断,但不打印线程消息。任何人都可以提出什么问题,因为我有一个类似的程序,昨天正常运行。
提前感谢!

The process_thread is interrupted on "ENTER" but is not printing the "exit process thread message". Can anyone suggest what the issue could be, because I got a similar program running properly yesterday. Thanks in advance!

推荐答案

运行 process_val 的线程只睡眠200ms,您可以在程序启动后按下小于200ms的键,该线程已返回,并且try / catch不再生效。如果你将睡眠时间增加到几千毫秒,你应该有时间在等待时按下一个键。

The thread running process_val is sleeping for only 200ms, so unless you can press a key less than 200ms after the program starts, that thread has already returned and the try/catch is no longer in effect. If you increase the sleep to a few thousand ms, you should have time to press a key while it's waiting.

这篇关于升压线程在中断时不打印退出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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