Boost logo

Boost :

From: William Kempf (williamkempf_at_[hidden])
Date: 2001-08-17 13:48:45


>From: "William Kempf" <williamkempf_at_[hidden]>
>>From: "Peter Dimov" <pdimov_at_[hidden]>
>> >From: "William Kempf" <williamkempf_at_[hidden]>
>> >>I'd prefer to
>> >>leave join() as an explicit operation. The destructor shall either
>>detach
>> >>the thread if it is still "joinable", or do nothing if it's been
>>joined.
>>
>> >How would you implement this? When a default-constructed thread object
>>is
>> >destroyed it doesn't know whether the thread has been already joined.
>>
>>Read the description a little closer. A "default-constructed thread
>>object"
>>is not joinable. Only the thread object that actually creates the thread
>>is
>>joinable (and only until it calls join() once). In actuality this design
>>is
>>by far easier to implement, especially on POSIX threads.
>
>The thread object creates a thread. Within that thread, a
>default-constructed thread object is created with "new thread", then passed
>to someone else. The original thread object (which is joinable) performs a
>join. Then that someone else deletes the default-constructed thread object.
>It detaches, since it doesn't know that a join has already been performed.
>Undefined behavior.

You still need to read closer. Here's a quote from the original message:

"The destructor shall either detach the thread if it is still "joinable", or
do nothing if it's been joined."

Granted, the wording would have been better as:

"The destructor shall either detach the thread if it is still "joinable",
else do nothing."

But I believe the intent was still clear enough. In your example the thread
object created through "new thread" was created as not joinable, so when the
destructor is run it does not detach the thread. I've got a working
implementation of this right now, and it was much easier to create than any
of the previous designs and does not produce any undefined behavior.

Bill Kempf

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


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