Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

When to unlock for using pthread_cond_signal()?

unlock
0
Posted

When to unlock for using pthread_cond_signal()?

0

POSIX specifically allows that a condition variable may be signalled or broadcast with the associated mutex either locked or unlocked. (Or even locked by someone else.) It simply doesn’t matter. At least, signalling while not holding the mutex doesn’t make the program in any way illegal. A condition variable is just a communication mechanism to inform waiters of changes in shared data “predicate” conditions. The predicate itself IS shared data, and must be changed in a way that’s thread-safe. In most cases, this means that you must hold the mutex when you change the data. (But you could also have a predicate like “read() returns data”, so that you could write data, signal the condition variable — and the waiter(s) would simply loop on the condition wait until read() returns some data.) The signal doesn’t need to be synchronized with the predicate value. What you DO need to synchronize is SETTING the predicate and TESTING the predicate. Given that basic and obvious requirement (it’s sh

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123