How to wait and notify like in Java In C/C++ for shared memory between two or more thread?I use pthread library.
The wait() method is defined in the Object class. The notify() method is defined in the Object class. 4. The wait() method is used for interthread communication. The notify() method is used to wake up a single thread.
To wait on a condition variable you use: pthread_cond_wait(&myConVar , &mymutex); You need to lock the mutex before calling the function. This means that the thread might wait if another thread is trying to use the condition variable.
wait() It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify() . The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method.
Condition Variable is a kind of Event used for signaling between two or more threads. One or more thread can wait on it to get signaled, while an another thread can signal this.
Instead of the Java object that you would use to wait/notify, you need two objects: a mutex and a condition variable. These are initialized with pthread_mutex_init
and pthread_cond_init
.
Where you would have synchronized on the Java object, use pthread_mutex_lock
and pthread_mutex_unlock
(note that in C you have to pair these yourself manually). If you don't need to wait/notify, just lock/unlock, then you don't need the condition variable, just the mutex. Bear in mind that mutexes are not necessarily "recursive", This means that if you're already holding the lock, you can't take it again unless you set the init flag to say you want that behaviour.
Where you would have called java.lang.Object.wait
, call pthread_cond_wait
or pthread_cond_timedwait
.
Where you would have called java.lang.Object.notify
, call pthread_cond_signal
.
Where you would have called java.lang.Object.notifyAll
, call pthread_cond_broadcast
.
As in Java, spurious wakeups are possible from the wait functions, so you need some condition which is set before the call to signal, and checked after the call to wait, and you need to call pthread_cond_wait
in a loop. As in Java, the mutex is released while you're waiting.
Unlike Java, where you can't call notify
unless you hold the monitor, you can actually call pthread_cond_signal
without holding the mutex. It normally doesn't gain you anything, though, and is often a really bad idea (because normally you want to lock - set condition - signal - unlock). So it's best just to ignore it and treat it like Java.
There's not really much more to it, the basic pattern is the same as Java, and not by coincidence. Do read the documentation for all those functions, though, because there are various flags and funny behaviours that you want to know about and/or avoid.
In C++ you can do a bit better than just using the pthreads API. You should at least apply RAII to the mutex lock/unlock, but depending what C++ libraries you can use, you might be better off using a more C++-ish wrapper for the pthreads functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With