简体   繁体   English

不间断地构建Boost.Thread

[英]Building Boost.Thread without interruption

How can I build boost.thread without default interruption point. 如何在没有默认中断点的情况下构建boost.thread。 I think that my application crash in a predefined interruption points. 我认为我的应用程序在预定义的中断点崩溃。 I'm using boost 1.53.0 with msvc10 我正在使用带有msvc10的boost 1.53.0

I've the following code 我有以下代码

class IOController {
public:
    IOController() { mThread = boost::thread( boost::bind( &IOController::poll, this ) ); }
    ~IOController() {mThread.interrupt(); mThread.join() }

    void doA() { boost::lock_guard<boost::mutex> lock( mMutex);  }

private:
    void ICanThrow()
    {
        try
        {
            boost::lock_guard<boost::mutex> lock( mMutex); 
            callFunctionWithSleepFor(); // calling function that can throw and use boost::sleep_for
        }
        catch( boost::system_error&) {}
        catch( std::exception& ) {}
        catch ( ... ) { /* APPLICATION CRASH. */ }
    }
    // this is a thread: mThread = boost::thread(&IOController::poll, this) on ctor
    void poll()
    {
        while(true)
        {
            callFunctionWithSleepFor( );            
            this_thread::sleep_for( some_time );
        }
    }
boost::mutex mMutex;
boost::thread mThread;
};

Now I've the main thread that is calling that is calling doA in a very intensive way, and the class is polling on another thread. 现在我调用的主线程是以非常密集的方式调用doA,并且该类在另一个线程上进行轮询。 But sometimes I caght an exception in ICanThrow in the catch (...). 但有时我会在ICanThrow中捕获异常(...)。 I've no idea why this happen. 我不知道为什么会这样。 But happen always from poll() thread. 但总是从poll()线程发生。

Now I want try to build Boost without DONT_PROVIDE_INTERRUPTIONS. 现在我想尝试在没有DONT_PROVIDE_INTERRUPTIONS的情况下构建Boost。 Does someone have some suggest? 有人有一些建议吗?

Maybe have a look at http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_management.html . 也许看看http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_management.html You can disable thread interruption with the disable_interruption class. 您可以使用disable_interruption类禁用线程中断。 I haven't used it myself, but it looks like if you instantiate disable_interruption , interruptions should be disabled until the disable_interruption object goes out of scope. 我自己没有使用它,但看起来如果你实例化disable_interruption ,应该禁用中断,直到disable_interruption对象超出范围。

The issue is that when the destructor of IOController call mThread.interrupt() a function elsewere that is using sleep_for throw the exception. 问题是当IOController的析构函数调用mThread.interrupt()一个函数else正在使用sleep_for抛出异常。 this exception is caght in the caught(...) but relly is just boost::thread_interrupted a class non deriving from anything. caught(...)这个例外很严重,但仅仅是boost::thread_interrupted一个非派生类的类。 The problem now is that thread_interrupted is not caght in the poll method so application is blocked on mThread.join() 现在的问题是在poll方法中thread_interrupted不紧密,因此应用程序在mThread.join()上被阻止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM