[c++11]多线程编程(四)——死锁(Dead Lock)

news/2024/7/1 12:15:55

死锁

如果你将某个mutex上锁了,却一直不释放,另一个线程访问该锁保护的资源的时候,就会发生死锁,这种情况下使用lock_guard可以保证析构的时候能够释放锁,然而,当一个操作需要使用两个互斥元的时候,仅仅使用lock_guard并不能保证不会发生死锁,如下面的例子:

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;

class LogFile {
    std::mutex _mu;
    std::mutex _mu2;
    ofstream f;
public:
    LogFile() {
        f.open("log.txt");
    }
    ~LogFile() {
        f.close();
    }
    void shared_print(string msg, int id) {
        std::lock_guard<std::mutex> guard(_mu);
        std::lock_guard<std::mutex> guard2(_mu2);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
    void shared_print2(string msg, int id) {
        std::lock_guard<std::mutex> guard(_mu2);
        std::lock_guard<std::mutex> guard2(_mu);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
};

void function_1(LogFile& log) {
    for(int i=0; i>-100; i--)
        log.shared_print2(string("From t1: "), i);
}

int main()
{
    LogFile log;
    std::thread t1(function_1, std::ref(log));

    for(int i=0; i<100; i++)
        log.shared_print(string("From main: "), i);

    t1.join();
    return 0;
}

运行之后,你会发现程序会卡住,这就是发生死锁了。程序运行可能会发生类似下面的情况:

Thread A              Thread B
_mu.lock()             _mu2.lock()
   //死锁               //死锁
_mu2.lock()         _mu.lock()

解决办法有很多:

  1. 可以比较mutex的地址,每次都先锁地址小的,如:

    if(&_mu < &_mu2){
        _mu.lock();
        _mu2.unlock();
    }
    else {
        _mu2.lock();
        _mu.lock();
    }
  2. 使用层次锁,将互斥锁包装一下,给锁定义一个层次的属性,每次按层次由高到低的顺序上锁。

这两种办法其实都是严格规定上锁顺序,只不过实现方式不同。

c++标准库中提供了std::lock()函数,能够保证将多个互斥锁同时上锁,

std::lock(_mu, _mu2);

同时,lock_guard也需要做修改,因为互斥锁已经被上锁了,那么lock_guard构造的时候不应该上锁,只是需要在析构的时候释放锁就行了,使用std::adopt_lock表示无需上锁:

std::lock_guard<std::mutex> guard(_mu2, std::adopt_lock);
std::lock_guard<std::mutex> guard2(_mu, std::adopt_lock);

完整代码如下:

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;

class LogFile {
    std::mutex _mu;
    std::mutex _mu2;
    ofstream f;
public:
    LogFile() {
        f.open("log.txt");
    }
    ~LogFile() {
        f.close();
    }
    void shared_print(string msg, int id) {
        std::lock(_mu, _mu2);
        std::lock_guard<std::mutex> guard(_mu, std::adopt_lock);
        std::lock_guard<std::mutex> guard2(_mu2, std::adopt_lock);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
    void shared_print2(string msg, int id) {
        std::lock(_mu, _mu2);
        std::lock_guard<std::mutex> guard(_mu2, std::adopt_lock);
        std::lock_guard<std::mutex> guard2(_mu, std::adopt_lock);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
};

void function_1(LogFile& log) {
    for(int i=0; i>-100; i--)
        log.shared_print2(string("From t1: "), i);
}

int main()
{
    LogFile log;
    std::thread t1(function_1, std::ref(log));

    for(int i=0; i<100; i++)
        log.shared_print(string("From main: "), i);

    t1.join();
    return 0;
}

总结一下,对于避免死锁,有以下几点建议:

  1. 建议尽量同时只对一个互斥锁上锁。

    {
        std::lock_guard<std::mutex> guard(_mu2);
        //do something
        f << msg << id << endl;
    }
    {
        std::lock_guard<std::mutex> guard2(_mu);
        cout << msg << id << endl;
    }
  2. 不要在互斥锁保护的区域使用用户自定义的代码,因为用户的代码可能操作了其他的互斥锁。

    {
        std::lock_guard<std::mutex> guard(_mu2);
        user_function(); // never do this!!!
        f << msg << id << endl;
    }
  3. 如果想同时对多个互斥锁上锁,要使用std::lock()
  4. 给锁定义顺序(使用层次锁,或者比较地址等),每次以同样的顺序进行上锁。详细介绍可看C++并发编程实战。

参考

  1. C++并发编程实战
  2. C++ Threading #4: Deadlock

http://www.niftyadmin.cn/n/4261164.html

相关文章

How to Change ASM Home on a Node in RAC [ID 558508.1]

How to Change ASM Home on a Node in RAC 步骤如下&#xff1a; 1&#xff09; Install a new Home to be used by ASM 2&#xff09; Stop the listener, database, asm on the node 3&#xff09; Listener is recommended to be run from asm home. We can reset the new…

销售前10的mysql_如何用sql语句将销量排名前十的商品查询

展开全部 1、创建测试表, create table test_sale(id varchar2(20),sale_num number(10)); 2、插入测试数据; insert into test_sale values(goods_1,15); insert into test_sale values(goods_2,125); insert into test_sale values(goods_3,28); insert into test_sale val…

SQL Server Configuration Performance Checklist

Performance Audit Checklist SQL ServerConfiguration SettingsAdvancedSetting?RequiresRestart?Default ValueCurrent Valueaffinity mask YesYes0 awe enabled YesYes0 cost threshold for parallelism Yes No 5 cursor threshold Yes No -1 fill factor (%) Yes Yes 0…

quick-cocos2dx 开发插件 QuickXDev vscode 版发布啦!

2019独角兽企业重金招聘Python工程师标准>>> 基本实现了 sublime 版的功能&#xff0c;目前适配 Quick-Cocos2dx-Community 3.7.2 版。 安装 在vscode扩展商店中搜索"QuickXDev"即可找到。 特性 cocos2dx c端的代码提示quick lua端的代码提示lua 5.1系统代…

xpage mysql_实例解决XPage访问出现HTTP403错误的问题

前段时间一直在尝试Domino8.5版本下的XPage的功能&#xff0c;但是浏览xsp文件一直会出现浏览器无法访问的情况&#xff0c;困扰了很久&#xff0c;网络上很少有关于XPage的资料&#xff0c;找不到答案&#xff0c;于是就搁浅下来了。今天偶然间发现了解决这个问题的办法&#…

今天解决了BXP4.1在windows server 2003下无法自动启动ardencepxe服务的问题

我的无盘系统一直工作的很好&#xff0c;可是最近总是无法自动启动ardencepxe服务&#xff0c;每次需要登录进入系统然后再手动启动一下&#xff0c;很是麻烦&#xff0c;上网搜了一下&#xff0c;发现盗版的bxp就是有这个问题&#xff0c;经过研究把问题终于解决了,方法如下&a…

vulcanjs schemas collections

一张参考图 说明 从上图我们可以方便的看出schmea 能做的事情 Generate a GraphQL equivalent of your schema to control your GraphQL API.&#xff08;生成 graphql api&#xff09;Control permissions for accessing and modifying data.&#xff08;访问控制&#xff09;…

loadrunner测试mysql数据库_Loadrunner测试mysql数据库

loadrunner可以利用mysql lib库&#xff0c;通过引用外部DLL&#xff0c;模拟mysql客户端连接数据库进行增删改查的操作进行测试。下面主要介绍如何利用mysql lib库连接mysql数据库进行性能测试。一、准备工作1. 下载 MySQL LoadRunner libraries。2. MySQL LoadRunner librari…