Boost logo

Boost :

Subject: [boost] [thread] thread::join() + thread::interrupt() not reliable?
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2013-01-14 04:34:26


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";
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk