[cs33211] sending notify_one and notify_all without holding the lock

Mikhail Nesterenko mikhail at cs.kent.edu
Thu Oct 18 18:00:56 EDT 2012


OS students,


A thread does not have to own a lock to execute notify_one() or
notify_all() to wake up other threads blocked on a condition variable.

However, it may lead to undesirable race conditions. Consider the the
following execution case:

  Thread 1              Thread 2

  mymutex.lock()
  ...                   ...
                        mycv.notify_all();
  ...                   ...
  mycv.wait(mymutex);   
  ...
  mymutex.unlock()


Thread 2 does not own mymutex and issues notify_all(). However, since
Thread 1 has not executed wait() yet, it misses notify_all(). To avoid
such race conditions, it is considered a good style to issue
notifications after acquiring the lock.

Thanks,
-- 
Mikhail


More information about the cs33211 mailing list