Suppose we have four threads in this order (D, C, B, A) waiting in a queue, and 'r.cv.notify_all()' just gets invoked, and suppose thread A (first thread in the queue) locks the mutex and the lambda returns true, after thread A reaches the end of the while loop, the lock is going to be released as it follows the RAII rule so my question is if the lock gets released at the end of the loop, is thread B going to acquire it, or still the lock belongs to thread A (as its job hasn't finished yet)
void operator() (SharedData& r, std::chrono::system_clock::time_point tToc)
{
unsigned short tempValue = r.value;
Debug::out("Befooooooooooooooooooreee \n");
while (std::chrono::system_clock::now() < tToc)
{
std::unique_lock<std::mutex> lock(r.mtx); // ---> "A B C D" four threads
if (r.cv.wait_until(lock, tToc, [&]
{
Debug::out("TEstttttttt -> temp %x \n", tempValue);
Debug::out("TEstttttttt -> value %x \n", r.value);
return (r.value != 0 && r.value != tempValue);
}
))
{
r.complement = ~(r.value);
Debug::out("AAAAAAAfterrrrrrrrrrfrrrr \n");
tempValue = r.value;
}
}
}