首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost disable_interruption不会阻止thread_interrupted

boost disable_interruption不会阻止thread_interrupted
EN

Stack Overflow用户
提问于 2011-08-08 06:16:15
回答 1查看 999关注 0票数 2

当线程在特定的作用域中时,我试图防止线程中断。但是,使用boost::this_thread::disable_interruption di()似乎没有任何效果。

代码语言:javascript
复制
#include <boost/thread.hpp>
#include <iostream>

void worker() {
    std::cout << "START" << std::endl;

    for(;;) {
        {
            boost::this_thread::disable_interruption di();
            try {
                boost::this_thread::sleep(boost::posix_time::seconds(1));
            }
            catch(boost::thread_interrupted & e) {
                assert( false );
            }
        }

        try {
            boost::this_thread::interruption_point();
        }
        catch(boost::thread_interrupted & e) {
            break;
        }

    }

    std::cout << "END" << std::endl;
}


int main() {
    boost::thread thread(&worker);
    thread.interrupt();
    thread.join();
}

文档似乎暗示,当di在作用域中时,boost::this_thread::sleep()不会抛出boost::thread_interrupted

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-08 06:33:08

您应该删除以下行中的括号:

代码语言:javascript
复制
//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;

您没有创建disable_interruption对象,而是声明了function di。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6976161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档