In multithreading (2 thread) program, I have this code:
while(-1)
{
m.lock();
(...)
m.unlock();
}
m
is a mutex (in my case a c++11 std::mutex
, but I think it'doesn't change if I use different library).
Assuming that the first thread owns the mutex and it's done something in (...)
part. The second thread tried to acquire the mutex, but it's waiting that the first thread release m
.
The question is: when thread 1 ends it's (...)
execution and unlocks the mutex, can we be sure that thread 2 acquires the mutex or thread 1 can re-acquire again the mutex before thread 2, leaving it stucked in lock()
?