In linux, how can synchronize between 2 thread (using pthreads on linux)?
I would like, under some conditions, a thread will block itself and then later on, it will be resume by another thread. In Java, there is wait(), notify() functions. I am looking for something the same on pthreads:
I have read this, but it only has mutex, which is kind of like Java’s synchronized keyword. That is not what I am looking for.
https://computing.llnl.gov/tutorials/pthreads/#Mutexes
Thank you.
You need a mutex, a condition variable and a helper variable.
in thread 1:
in thread 2:
The reason you need a helper variable is because condition variables can suffer from spurious wakeup. It’s the combination of a helper variable and a condition variable that gives you exact semantics and efficient waiting.