7

我无法在工作线程中捕获中断。这里有许多提升线程中断帖子,但是,它们似乎是愚蠢的错误(我敢肯定我的问题也是如此),或者没有帮助的东西

这里

这里

此示例崩溃

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

在我的示例中,有一个创建工作线程的主线程。主线程等待用户按下回车,而工作线程处于 while 循环中,并休眠 1 秒间隔。当用户按下进入主线程中断工作线程然后调用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;
}

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

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

其中前面的数字是线程 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.

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.

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

如何在工作线程中捕获 boost::thread_interrupted 以便主线程正常继续?同样,如果我使用 timed_wait() 而不是 sleep_for(),我是否需要考虑其他任何事情?

我正在使用 boost 1.53.0 和 MinGW 4.4.1。我正在为 boost::thread 和 boost::system 使用运行时链接静态多线程库

谢谢

4

0 回答 0