[thread] thread::join() + thread::interrupt() not reliable?

Hi, following code does not work reliable - sometimes the output is thread_interrupted thrown by t1 in main(): false thread_interrupted thrown by t1 in fn2(): false done and sometimes I get an exception terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error>
' what(): boost thread: thread not joinable: Invalid argument
I would expect that thread_interrupted will be catched in main() and fn2() as the documentation describes. Oliver #include <iostream> #include <boost/assert.hpp> #include <boost/bind.hpp> #include <boost/thread.hpp> void fn1( boost::barrier * b) { b->wait(); boost::this_thread::interruption_point(); } void fn2( boost::thread * t1, boost::barrier * b, bool * interrupted) { t1->interrupt(); b->wait(); try { t1->join(); } catch ( boost::thread_interrupted const&) { * interrupted = true; } } int main() { bool interrupted1 = false, interrupted2 = false; boost::barrier b( 2); boost::thread t1( boost::bind( fn1, & b) ); boost::thread t2( boost::bind( fn2, & t1, & b, & interrupted2) ); try { t1.join(); } catch ( boost::thread_interrupted const&) { interrupted1 = true; } t2.join(); std::cout << "thread_interrupted thrown by t1 in main(): " << std::boolalpha << interrupted1 << "\n"; std::cout << "thread_interrupted thrown by t1 in fn2(): " << std::boolalpha << interrupted2 << "\n"; std::cout << "done\n"; }

2013/1/14 Oliver Kowalke <oliver.kowalke@gmail.com>
void fn1( boost::barrier * b) { b->wait(); boost::this_thread::interruption_point(); }
void fn2( boost::thread * t1, boost::barrier * b, bool * interrupted) { t1->interrupt(); b->wait(); try { t1->join(); } catch ( boost::thread_interrupted const&) { * interrupted = true; } }
some correction -> replace void fn1( boost::barrier * b) { b->wait(); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC_); xt.sec += 1; //1 second boost::this_thread::sleep( xt); boost::this_thread::interruption_point(); } void fn2( boost::thread * t1, boost::barrier * b, bool * interrupted) { b->wait(); t1->interrupt(); try { t1->join(); } catch ( boost::thread_interrupted const&) { * interrupted = true; } } thread_interrupted is not catched

answering myself: postconditions of thread::join(): If *this refers to a thread of execution on entry, that thread of execution has completed. *this no longer refers to any thread of execution. note: Operations on *this are not synchronized. you can not expect to call thread::join() from multiple threads.

Le 14/01/13 16:13, Oliver Kowalke a écrit :
answering myself: postconditions of thread::join(): If *this refers to a thread of execution on entry, that thread of execution has completed. *this no longer refers to any thread of execution. note: Operations on *this are not synchronized. you can not expect to call thread::join() from multiple threads. I confirm.
Vicente
participants (2)
-
Oliver Kowalke
-
Vicente J. Botet Escriba