Multithreading OSX libstdc++;防止boost::thread中断?

Multithreading OSX libstdc++;防止boost::thread中断?,multithreading,c++11,boost,libstdc++,libc++,Multithreading,C++11,Boost,Libstdc++,Libc++,考虑以下示例代码,该代码创建线程并使用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

考虑以下示例代码,该代码创建线程并使用
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;
}
但是,如果将使用libstdc++:

g++main.cpp-I-std=gnu++11-stdlib=libstdc++-lboost\u thread-mt-lboost\u system-mt-lboost\u chrono-mt-L/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上复制了以下内容:

  • 对于GCC4.8和4.9,它可以正常工作
  • 使用Clang3.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开发人员可能会指出这是libc++中的一个bug

我不排除这种可能性(中断点与标准库/OS功能的关系相当复杂)。值得在Boost-Trac上归档,但肯定要针对Boost归档一个bug。
interruption enabled 128
thread function
oops!.. time to finish!
joinable: 1
Segmentation fault (core dumped)