Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::condition_variable wait without a lock

Is it possible to "wait" on a boost::condition_variable without having to acquire a mutex lock first? Failing that, can this be done using the pthread lib directly somehow?

For simplicity I'd like to avoid going directly to the OS layer (such as futex on linux). But I also don't want the overhead of the mutex call.

I'm quite aware of the race conditions doing this under normal circumstances. I have that covered in another fashion.

like image 428
edA-qa mort-ora-y Avatar asked May 23 '26 22:05

edA-qa mort-ora-y


1 Answers

The mutex must be acquired first in both case.

From boost.threads documentation :

void wait(boost::unique_lock& lock)

Precondition: lock is locked by the current thread [...]

From pthread_cond_wait man page :

The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.

I'm not aware of any alternative.

like image 175
icecrime Avatar answered May 26 '26 12:05

icecrime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!