为什么这个 C++11 程序不会死锁?
Why is this C++11 program not going to deadlock?
#include <iostream>
#include <mutex>
using namespace std;
int main()
{
mutex m;
m.lock();
cout << "locked once\n";
m.lock();
cout << "locked twice\n";
return 0;
}
输出:
./a.out
locked once
locked twice
程序是否需要在第二次锁定时死锁,即互斥体被同一线程锁定两次?
If lock is called by a thread that already owns the mutex, the behavior is undefined: the program may deadlock, or, if the implementation can detect the deadlock, a resource_deadlock_would_occur error condition may be thrown.
#include <iostream>
#include <mutex>
using namespace std;
int main()
{
mutex m;
m.lock();
cout << "locked once\n";
m.lock();
cout << "locked twice\n";
return 0;
}
输出:
./a.out
locked once
locked twice
程序是否需要在第二次锁定时死锁,即互斥体被同一线程锁定两次?
If lock is called by a thread that already owns the mutex, the behavior is undefined: the program may deadlock, or, if the implementation can detect the deadlock, a resource_deadlock_would_occur error condition may be thrown.