Boost logo

Boost :

Subject: Re: [boost] [thread 1.48] Multiple interrupt/timed_join leads to deadlock
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2012-12-05 12:08:24


On 05/12/12 15:59, Gaetano Mendola wrote:
> On 05/12/2012 16.29, Vicente Botet wrote:
>> template<typename TimeDuration>
>> bool timed_join(TimeDuration const& rel_time);
>>
>> Preconditions:
>>
>> this->get_id()!=boost::this_thread::get_id()
>>
>> Postconditions:
>>
>> If *this refers to a thread of execution on entry, and timed_join
>> returns true, that thread of execution has completed, and *this no longer
>> refers to any thread of execution. If this call to timed_join returns
>> false,
>> *this is unchanged.
>> "
>>
>> Your second call doesn't satisfy the pre-conditions, so that the
>> outcome of
>> this second call is undefined.
>
> That precondition tests that your are not interrupting yourself doesn't
> say anything about thread safety. Am I missing something ?

Your code had two bugs. Firstly, it called interrupt and timed_join on
the same thread object from multiple threads. Secondly, it called
interrupt and timed_join in a loop without ever checking the result of
timed_join.

Vicente's quote from the docs addresses the second issue: if timed_join
returns true then looping round to call interrupt and timed_join again
is now a precondition violation, and thus undefined behaviour.

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++11 thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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