开发者

boost::condition_variable wait without a lock

Is it possible to "wait" on 开发者_JAVA技巧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.


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.


No, it is not. As you can see from the condition_variable API specs it always needs to be provided with a lockable packed in a unique_lock. But I don't really understand where the problem is. This construct implements a a "monitor", which can't be done without some kind of synchonization object...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜