c++ thread 线程死锁

#include <iostream>
#include <list>
#include <thread>
#include <mutex>

using namespace std;

int n = 10;
class A
{
public:
    // 把收到的消息入到一个队列的线程
    void inMsgRecvQueue()
    {
        for (int i = 0; i < n; ++i)
        {
            cout << "in Queue:" << i << endl;
            my_mutex1.lock(); // 实际工程这两个
            my_mutex2.lock();
            this->msgRecvQueue.push_back(i); // 假设这个数字i就是我收到的命令,我直接弄到消息队列里边来
            my_mutex2.unlock();
            my_mutex1.unlock();
        }
    }

    bool outMsgLULProc(int &command)
    {
        my_mutex2.lock();
        my_mutex1.lock();
        if (!this->msgRecvQueue.empty())
        {
            // 消息不为空
            command = this->msgRecvQueue.front(); // 返回第一个元素,但不检查元素是否存在;
            this->msgRecvQueue.pop_front();       // 移除第一个元素,但不返回
            my_mutex1.unlock();
            my_mutex2.unlock();
            return true;
        }
        my_mutex1.unlock();
        my_mutex2.unlock();
        return false;
    }

    // 把数据从消息队列中取出的线程
    void outMsgRecvQueue()
    {
        int command = 0;
        // 用while
        while (1)
        {
            bool result = outMsgLULProc(command);
            if (result == true)
            {
                cout << "out Queue: " << command << endl;
                // 进行数据处理
                // ....
            }

            //  当全部读取完之后退出
            if (command == n - 1)
                break;
        }
        cout << "end" << endl;
    }

private:
    std::list<int> msgRecvQueue; // 容器(消息队列),专门用于代表玩家给咱们发送过来的命令
    std::mutex my_mutex;         // 创建一个互斥量
    std::mutex my_mutex1;        // 第一把锁
    std::mutex my_mutex2;        // 第二把锁
};

class B
{
private:
    std::list<int> msg_queue; // 容器(消息队列),专门用于代表玩家给咱们发送过来的命令
    std::mutex mtx1;
    std::mutex mtx2;

public:
    // 把收到的消息入到一个队列的线程
    void inMsgRecvQueue();
    // 把数据从消息队列中取出的线程
    void outMsgRecvQueue();
    bool outMsgLULProc(int &command);
};

// 把收到的消息入到一个队列的线程
void B::inMsgRecvQueue()
{
    for (int i = 0; i < n; ++i)
    {
        std::lock_guard<std::mutex> lock1(mtx1);
        std::lock_guard<std::mutex> lock2(mtx2);
        cout << "in Queue:" << i << endl;
        msg_queue.push_back(i); // 假设这个数字i就是我收到的命令,我直接弄到消息队列里边来
    }
}

bool B::outMsgLULProc(int &command)
{
    std::lock_guard<std::mutex> lock2(mtx2); // mtx2
    std::lock_guard<std::mutex> lock1(mtx1); // mtx1
    if (!msg_queue.empty())
    {
        // 消息不为空
        command = msg_queue.front(); // 返回第一个元素,但不检查元素是否存在;
        msg_queue.pop_front();       // 移除第一个元素,但不返回
        return true;
    }
    return false;
}

// 把数据从消息队列中取出的线程
void B::outMsgRecvQueue()
{
    int command = 0;
    // 用while
    while (1)
    {
        bool result = outMsgLULProc(command);
        if (result == true)
        {
            cout << "out Queue: " << command << endl;
            // 进行数据处理
            // ....
        }
        //  当全部读取完之后退出
        if (command == n - 1)
            break;
    }
    cout << "end" << endl;
}

void dead_lock_test2()
{
    cout << "dead_lock_test2" << endl;
    B b;
    std::thread OutMsgObj(&B::outMsgRecvQueue, &b); // 成员函数作为线程的入口函数
    std::thread InMsgObj(&B::inMsgRecvQueue, &b);

    OutMsgObj.join();
    InMsgObj.join();
}

int main(int argc, char const *argv[])
{
    // dead_lock_test1();
    dead_lock_test2();
    system("pause");

    return 0;
}
/*
maybe1:
dead_lock_test2

maybe2:
dead_lock_test2
in Queue:0
in Queue:1
in Queue:2
in Queue:3
in Queue:4
in Queue:5
in Queue:6
in Queue:7
in Queue:8
in Queue:9
out Queue: 0
out Queue: 1
out Queue: 2
out Queue: 3
out Queue: 4
out Queue: 5
out Queue: 6
out Queue: 7
out Queue: 8
out Queue: 9
end
*/

参考:https://www.cnblogs.com/Balcher/p/16630589.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值