问题
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;
}
Now, when compiled on OS X 10.10.3 like this (libc++ is used by default):
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
the output shows that interruptions are enabled:
interruption enabled 1
thread function
interruption enabled 1
thread function
...
If, however, one will use 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
I get the output which tells me that interruption are disabled:
interruption enabled 0
thread function
interruption enabled 0
thread function
...
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
Thanks
回答1:
I've reproduced this on linux:
- 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
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.
There is a chance that the boost devs will indicate it's a bug in libc++
来源:https://stackoverflow.com/questions/30901904/os-x-libstdc-prevents-boostthread-from-interruptions