Tags

links:
http://en.wikipedia.org/wiki/Spurious_wakeup
http://javarevisited.blogspot.com/2013/12/inter-thread-communication-in-java-wait-notify-example.html
http://stackoverflow.com/questions/14924469/does-pthread-cond-waitcond-t-mutex-unlock-and-then-lock-the-mutex
http://stackoverflow.com/questions/6206350/how-to-use-condition-variable

summary:
– happens in java wait/notify or pthread_cond_wait
– need a while loop checking condition again, due to spurious wakeup; or the possibility of more than 1 thread gets waken up by pthread_cond_signal
– pthread_cond_wait() actually unlocks mutex, wait for condition, then locks mutex again

java example, note: need to sync, wait, notify on the same object.

synchronized (object) {   
      while (condition check)
             object.wait();         
      object.doSomething(); 
      object.notify(); // notify the other waiting thread
}
Advertisements