Opened 10 years ago

Closed 10 years ago

#7496 closed Feature Requests (worksforme)

[thread] call interrupt() and join() int destructor of a joinable thread

Reported by: Andrzej Krzemienski <akrzemi1@…> Owned by: viboes
Milestone: To Be Determined Component: thread
Version: Boost 1.52.0 Severity: Problem
Keywords: thread interrupt join destructor Cc:

Description

boost::thread's destructor calls terminate if joinable, in order to conform to C++11 specification. I am not sure if this is the best course of action. My understanding -- form the C++ Committee papers and informal presentations -- is that the reason for introducing a 'terminating destructor' was the lack of thread cancellation/interruption functionality. Thread interruption is supposed to be the preferred behavior for thread's destructor. std::thread does not support interruption (for some reasons), but boost::thread does (this is already a departure from C++11), so shouldn't the latter prefer to interrupt and join a joinable thread in the destructor?

My preference would be to join (rater than detach) after the interruption. If I remember correctly, the argument against joining for std::thread is that there would be an unexpected hang upon reaching the end of the scope. The argument against detaching for std::thread is that the detached thread may be holding references to automatic variables defined in the scope of the forking thread that we are now exiting.

I believe that with thread interruption in place the argument against joining is mitigated, while the argument against detaching still holds.

Change History (2)

comment:1 by viboes, 10 years ago

Owner: changed from Anthony Williams to viboes
Status: newassigned

comment:2 by viboes, 10 years ago

Resolution: worksforme
Status: assignedclosed

After discussion on the ML (http://boost.2283326.n4.nabble.com/thread-terminating-destructor-tt4636872.html) it seems that this request is more needed.

I've created a ticket that should help the user to get what they need at destruction

#7541 Add a thread wrapper class that joins on destruction

Note that the resolution will be to parameterize the action to be done at destruction so that the user can easily do a interrupt and join.

Note: See TracTickets for help on using tickets.