OS X的libstdc ++ prevents的boost ::从中断线程? [英] OS X libstdc++ prevents boost::thread from interruptions?
问题描述
考虑下面的示例code这将创建一个线程,并使用从主线程中断,螺纹::中断
电话:
Consider the following sample code which creates a thread and interrupts it from the main thread using thread::interrupt
call:
#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 ++默认情况下使用):
Now, when compiled on OS X 10.10.3 like this (libc++ is used by default):
G ++的main.cpp -I&LT; path_to_boost_1_54_0&GT; -std = GNU ++ 11 -lboost_thread-MT -lboost_system-MT -lboost_chrono-MT -L&LT; path_to_boost_1_54_0&GT; /台/ lib目录
输出显示中断已启用:
interruption enabled 1
thread function
interruption enabled 1
thread function
...
如果,然而,人们将使用的libstdc ++:
If, however, one will use libstdc++:
G ++的main.cpp -I&LT; path_to_boost_1_54_0&GT; -std = GNU + 11 = -stdlib ++的libstdc -lboost_thread-MT -lboost_system-MT -lboost_chrono-MT -L&LT; path_to_boost_1_54_0&GT; /台/ lib目录
我得到它告诉我,中断输出将被禁用:
I get the output which tells me that interruption are disabled:
interruption enabled 0
thread function
interruption enabled 0
thread function
...
是否有这种行为的原因吗?我用升压v1.54.0这LLVM V6.1.0:
Is there a reason for such behaviour? I used boost v1.54.0 and this 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上:
I've reproduced this on linux:
- 用gcc 4.8,4.9它的工作原理
-
铿锵3.5和libstdc ++它的工作原理
- with gcc 4.8, 4.9 it works
with clang 3.5 and libstdc++ it works
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
铿锵3.5和的libc ++这是行不通的:
with clang 3.5 and libc++ it doesn't work:
interruption enabled 128
thread function
oops!.. time to finish!
joinable: 1
Segmentation fault (core dumped)
此问题应在提升列表中的报道,在我看来。
This issue should be reported at the boost list, in my opinion.
有一个机会,助推开发者将表明它在libc中的一个错误++
There is a chance that the boost devs will indicate it's a bug in libc++
这篇关于OS X的libstdc ++ prevents的boost ::从中断线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!