linux - 为什么这个 C++11 程序不会死锁?

标签 linux c++11 pthreads mutex 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.

http://en.cppreference.com/w/cpp/thread/mutex/lock

关于linux - 为什么这个 C++11 程序不会死锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059303/

相关文章:

c - Pthreads - 为什么 C 函数被声明为 void*?

c++ - 在网站上:godbolt.org,只有在那里:如何使用 std::thread?

linux - 使用 Linux/Bash 对空格分隔的数字进行排序

c++ - poll() 系统调用以及之后接收或发送数据的行为

linux - Kohana_HTTP_Exception [404] : The requested URL/was not found on this server

c++ - 如何拥有具有类型和大小的可变模板?

c++ - 如何 `decltype` 仿函数运算符()?

c++ - 为什么这段代码会多次输出相同的数字?

linux - 具体到linux 2.6.30.5中的task_struct

windows - 通过另一个程序向 GUI 应用程序提供输入