如何在工作线程中捕获boost :: thread_interrupted异常?

And*_*dre 5 c++ multithreading boost-thread

我在工作线程中捕获中断时遇到问题.这里有很多提升线程中断帖子但是,它们似乎是愚蠢的错误(我确定我的问题也是如此),或者什么都没有帮助

这里

这里

这个例子崩溃了

我的例子是基于此,它仍然崩溃

在我的示例中有一个创建工作线程的主线程.主线程等待用户在工作线程处于while循环时按Enter键,并以1秒的间隔休眠.当用户按下enter键时,主线程中断工作线程,然后调用join,工作线程应该在boost :: this_thread :: sleep_for(sec)处抛出一个thread_interrupted异常,应该被捕获,然后工作者函数应该退出.这允许主线程继续然后退出程序.

#define BOOST_THREAD_USE_LIB 1
#define BOOST_SYSTEM_NO_DEPRECATED 1
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501

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


using namespace std;

void ThreadFunctionSleep()
{
    cout <<  boost::this_thread::get_id() << " worker thread " << endl;
    int counter = 0;

    while(1)
    {
        cout <<  boost::this_thread::get_id() << " thread iteration " << ++counter << " Press Enter to stop" << endl;

        try
        {
            boost::chrono::seconds sec(1);
            boost::this_thread::sleep_for(sec);
        }
        catch(boost::thread_interrupted&)
        {
            cout <<  boost::this_thread::get_id() << " Thread is stopped in ThreadFunction " << endl;
            return;
        }
        catch ( boost::thread_resource_error &)
        {
            cout <<  boost::this_thread::get_id() << " boost thread_resource_error" << endl;
            return;
        }
        catch(...)
        {
            cout <<  boost::this_thread::get_id() << " worker func other" << endl;
            return;
        }

    }




}

int main()
{
    std::cout <<  boost::this_thread::get_id() << " main thread " << std::endl;

    // Start thread
    boost::thread t(&ThreadFunctionSleep);


    // Wait for Enter
    std::cout <<  boost::this_thread::get_id() << " Waiting for Enter" << std::endl;
    char ch;
    cin.get(ch);

    // Ask thread to stop
    //std::cout <<  t.get_id() << " joinable " << t.joinable() << std::endl;
    try
    {
        t.interrupt();
    }
    catch(boost::thread_interrupted&)
    {
        std::cout <<  boost::this_thread::get_id() << " Thread is stopped" << std::endl;

    }
    catch ( boost::thread_resource_error &)
    {
        std::cout <<  boost::this_thread::get_id() << " boost thread_resource_error" << std::endl;

    }



    // Join - wait when thread actually exits
    try
    {
        t.join();

    }
    catch(boost::thread_interrupted&)
    {
        std::cout <<  boost::this_thread::get_id() << " Thread is joined" << std::endl;

    }
    catch(boost::system::system_error &)
    {
        std::cout <<  boost::this_thread::get_id() << " boost system_error" << std::endl;

    }
    catch(...)
    {
        std::cout <<  boost::this_thread::get_id() << " other exception" << std::endl;

    }

    cout <<  boost::this_thread::get_id() << " main: thread ended" << endl;




    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望我的程序输出是这样的:

4f4 main thread
1264 worker thread
4f4 Waiting for Enter
1264 thread iteration 1 Press Enter to stop
1264 thread iteration 2 Press Enter to stop
1264 Thread is stopped in ThreadFunction 
4f4 main: thread ended
Run Code Online (Sandbox Code Playgroud)

前面的数字是线程ID.问题是,我根本没有看到这一点.按Enter键后,代码执行有3种不同的方式.这表明我没有看到某种多线程问题.

1)在t.join()之前没有捕获异常,工作线程继续执行主线程

11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
 Waiting for Enter
644 thread iteration 2 Press Enter to stop

644 Thread is joined
644 main: thread ended

Process returned 0 (0x0)   execution time : 2.750 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)

2)程序崩溃

11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
 Waiting for Enter
644 thread iteration 2 Press Enter to stop

644 Thread is joined
644 main: thread ended

Process returned 0 (0x0)   execution time : 2.750 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)

3)很少发生,但无论如何都会崩溃

11cc main thread
11cc644 worker thread
644 thread iteration 1 Press Enter to stop
 Waiting for Enter
644 thread iteration 2 Press Enter to stop

644 Thread is stopped in ThreadFunction
Run Code Online (Sandbox Code Playgroud)

如何在工作线程中捕获boost :: thread_interrupted,以便主线程将继续正常?在类似的说明中,如果我使用timed_wait()而不是sleep_for(),我还需要考虑其他任何事情吗?

我使用的是boost 1.53.0和MinGW 4.4.1.我正在为boost :: thread和boost :: system使用runtime-link-static,多线程库

谢谢