On Jul 28, 2008, at 6:49 AM, Vladimir Pozdyaev wrote:
Anthony Williams:
"Peter Dimov"
writes: Anthony Williams:
However, it is never safe to destroy any object whilst another thread might still be in a member function.
I believe that this is a "POSIX destruction safety" issue. POSIX CVs are supposed to support such code.
POSIX CVs are supposed to support broadcast immediately before destroy. What we have here is the waiting thread doing the destruction, which is not guaranteed safe by POSIX, as far as I know.
Looking at, e.g., http://www.opengroup.org/onlinepubs/009695399/functions/pthread_cond_init.ht... reveals that "It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked." Am I right in understanding that the wait-destroy sequence should be quite safe this way (provided no one else waits)?
I believe your code as shown is subject to "spurious wakeup". If the
wait ends (spuriously) before the notify_all() completes, or is even
initiated, then the notify happens on a destructed cv. POSIX says
that in this case the pthread_cond_broadcast will return EINVAL.
The sentence you refer to means that the thread doing the notifying
can safely destroy the cv even if the waiting thread has not yet
returned from the wait (as long as that thread or any other do not try
to initiate a new wait after the final signal/broadcast). Note though
that it is not safe to destruct the mutex which is being waited on,
until the waiting thread wakes from the wait (locking the mutex) and
then unlocks that mutex.
I recommend:
#include