gpt4 book ai didi

multithreading - OS X libstdc++可以防止boost::thread中断?

转载 作者:行者123 更新时间:2023-12-03 13:04:37 25 4
gpt4 key购买 nike

考虑以下示例代码,该示例代码创建一个线程并使用thread::interrupt调用从主线程中中断该线程:

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

int main()
{
boost::thread t([]{
int counter = 0;

while (1){
std::cout << "interruption enabled " << boost::this_thread::interruption_enabled() << std::endl;
try {
counter++;

if (counter % 5 == 0)
throw std::runtime_error("runtime error!");

std::cout << "thread function\n";
boost::this_thread::sleep_for(boost::chrono::milliseconds(300));
}
catch (boost::thread_interrupted &interruption)
{
std::cout << "oops!.. time to finish!" << std::endl;
return;
//std::cout << "...but couldn't o_O..." << std::endl;
}
catch (std::exception &e)
{
std::cout << "some exception: " << e.what() << std::endl;
}
}
});

boost::this_thread::sleep_for(boost::chrono::milliseconds(3000));
t.interrupt();
std::cout << "joinable: " << t.joinable() << std::endl;
if (t.joinable())
if (!t.try_join_for(boost::chrono::milliseconds(500)))
{
std::cout <<"still RUNNING\n detach it..." << std::endl;
t.detach();
}

std::cout << "main thread\n";
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));

return 0;
}

现在,当像这样在OS X 10.10.3上编译时(默认使用libc++):
g++ main.cpp -I<path_to_boost_1_54_0> -std=gnu++11 -lboost_thread-mt -lboost_system-mt -lboost_chrono-mt -L<path_to_boost_1_54_0>/stage/lib
输出显示启用了中断:
interruption enabled 1
thread function
interruption enabled 1
thread function
...

但是,如果使用libstdc++:
g++ main.cpp -I<path_to_boost_1_54_0> -std=gnu++11 -stdlib=libstdc++ -lboost_thread-mt -lboost_system-mt -lboost_chrono-mt -L<path_to_boost_1_54_0>/stage/lib
我得到的输出告诉我禁用了中断:
interruption enabled 0
thread function
interruption enabled 0
thread function
...

有这种行为的原因吗?我使用了boost v1.54.0和此LLVM v6.1.0:
$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix

谢谢

最佳答案

我已经在linux上复制了它:

带有gcc 4.8、4.9的

  • ,它可以工作
  • 带有tlang 3.5和libstdc++的

  • interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    some exception: runtime error!
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    interruption enabled 1
    some exception: runtime error!
    interruption enabled 1
    thread function
    interruption enabled 1
    thread function
    joinable: 1
    oops!.. time to finish!
    main thread
  • 带clang 3.5和libc++的
  • 不起作用:
    interruption enabled 128
    thread function
    oops!.. time to finish!
    joinable: 1
    Segmentation fault (core dumped)

  • 我认为,应该在 boost 列表中报告此问题。

    Boost开发人员可能会指出这是libc++中的错误

    关于multithreading - OS X libstdc++可以防止boost::thread中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30901904/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com