The following example invokes a method foo()where it acquires ownership of the mutex and blocks it. Then it calls check(), which acquires ownership, but assumes that the mutex is already locked and therefore simply uses it with the help std::adopt_lock.
But when it check()ends, the mutex unlocks. So when it foo()continues, the section that I tried to protect is actually no longer protected.
#include <mutex>
static std::mutex sessionLock;
bool check();
void foo() {
std::lock_guard<std::mutex> guard(sessionLock);
if (check()) {
}
}
bool check() {
std::lock_guard<std::mutex> guard(sessionLock, std::adopt_lock);
return true;
}
int main() {
foo();
return 0;
}
. - std::adopt_lock (.. lock()), unlock()? , , - , .
std::recursive_mutex, , std::mutex, check(), ?