[cpp-threads] Thread try/time_join (was: Asynchronous FunctionProposal)

Howard Hinnant hinnant at twcny.rr.com
Wed Jun 3 23:24:48 BST 2009


On Jun 3, 2009, at 6:06 PM, Boehm, Hans wrote:

>> From:  Howard Hinnant
>>
>> I also note that boost::thread now supports timed_join, both
>> relative and absolute variants. ;-)
> My concern remains that all the obvious use cases for these seem to  
> be incorrect and some of them are potentially quite dangerous.   
> Unless we come up with really clever solutions for N2880.
>
>>
>> Still, once Anthony put interrupt() into boost::thread, he
>> pretty much doomed std::thread to DOA (and rightly so).
> Except that for now we still get a choice between absolutely no  
> interruption mechanism (std::thread) and detach-on-thread- 
> destruction resulting in stack overwrites on exception  
> (boost::thread).  Pick your poison :-( .
>
> I know I shouldn't suggest this, (for many reasons, including the  
> fact that I haven't thought it through enough) but what if we added  
> a modified version of boost's
> thread::interrupt() that implicitly included a join() call?  That  
> would remove a potential source of object-lifetime-related bugs.  It  
> would also make it very hard to ignore a thread_interrupted  
> exception for long.  Would it avoid the long-standing objections to  
> having such exceptions ignored?

interrupt & join, or at least interrupt & wait for acknowledgement of  
interrupt would make excellent ~thread() semantics in my opinion.  The  
only time ~thread() should have any work to do is when it executes  
during exception-driven stack unwinding.  However if there is no  
interrupt-and-don't-wait, then the following code is going to have  
extremely poor performance:

vector<thread> v;
try
{
    ...
}
catch (std::ive_been_interrupted&)
{
    for (auto i = v.begin(); i != v.end(); ++i)
         i->interrupt();                 // I don't want to wait here
    for (auto i = v.begin(); i != v.end(); ++i)
         i->join();                      // I want to wait here instead
    throw;                               // All v-threads should  
interrupt in parallel, not sequentially
}

If you put too many seat belts in the car, the driver won't be able  
move enough to safely drive it.  There has to be a reasonable  
compromise between safety and functionality.  Especially if we're  
threading for performance reasons.

-Howard




More information about the cpp-threads mailing list