gpt4 book ai didi

c++ - 互斥锁是否正常工作?不能死锁

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:37 24 4
gpt4 key购买 nike

所以我正在尝试编写一个应该死锁的 C++ 程序。使用我的以下代码,我相信它应该可以工作但不会死锁。是我的 mutex.lock() 没有正常工作吗?我的意思是如果 mut2.lock() 锁定关键部分,它应该解锁直到它正确完成意味着 funcA 不应该运行或至少等到 mut2.unlock() 因为它们都使用资源 rs2?为什么我不能使程序死锁?

#include <iostream>
#include <thread>
#include <mutex>
#include <pthread.h>
#include <cstdio> // getchar
#include <thread> // this_thread, yield
#include <future> // async
#include <chrono> // seconds
#include <unistd.h>
using namespace std;
mutex mut1, mut2, mut3;
int rs1=1; int rs2 = 2; int rs3=3;
int MAX = 20;

void funcA(){

mut1.lock();
cout<<"mut1 lock for thread A\n";
for(int i=0; i<MAX; i++){
rs2 = i;
cout<<"[Aloop]rs1: "<<rs1<<" rs2: "<<rs2<<" rs3: "<<rs3<<" i:"<<i << endl;
}
rs1 = rs2;
cout<<"[A]rs1: "<<rs1<<" rs2: "<<rs2<<" rs3: "<<rs3 << endl;
mut1.unlock();
cout<<"mut1 unlock for thread A\n";
}
void funcB(){

mut2.lock();
cout<<"mut2 lock for thread B\n";
rs3 = rs1 + rs2;
cout<<"[B]rs3: "<<rs3 << " rs1: "<<rs1 << " rs2: "<<rs2 <<endl;
sleep(50);
mut2.unlock();
cout<<"mut2 unlock for thread B\n";
}


int main(){
thread tA(funcA);
thread tB(funcB);

tA.join();
tB.join();

return 0;
}

//Here is my makefile
#Makefile project 2

project2: project2.o
g++ -std=c++11 -pthread -o project2 project2.o
project2.o: project2.cpp project2.h
g++ -std=c++11 -Wall -pthread -c project2.cpp
//Below is my output

mut2 lock for thread B

[B]rs3: 3 rs1: 1 rs2: 99

mut1 lock for thread A

[Aloop]rs1: 1 rs2: 0 rs3: 3 i:0

[Aloop]rs1: 1 rs2: 1 rs3: 3 i:1

[Aloop]rs1: 1 rs2: 2 rs3: 3 i:2

[Aloop]rs1: 1 rs2: 3 rs3: 3 i:3

[Aloop]rs1: 1 rs2: 5 rs3: 3 i:5

[Aloop]rs1: 1 rs2: 6 rs3: 3 i:6

[Aloop]rs1: 1 rs2: 7 rs3: 3 i:7

[Aloop]rs1: 1 rs2: 8 rs3: 3 i:8

[Aloop]rs1: 1 rs2: 9 rs3: 3 i:9

[Aloop]rs1: 1 rs2: 10 rs3: 3 i:10

[Aloop]rs1: 1 rs2: 11 rs3: 3 i:11

[Aloop]rs1: 1 rs2: 12 rs3: 3 i:12

[Aloop]rs1: 1 rs2: 13 rs3: 3 i:13

[Aloop]rs1: 1 rs2: 14 rs3: 3 i:14

[Aloop]rs1: 1 rs2: 15 rs3: 3 i:15

[Aloop]rs1: 1 rs2: 16 rs3: 3 i:16

[Aloop]rs1: 1 rs2: 17 rs3: 3 i:17

[Aloop]rs1: 1 rs2: 18 rs3: 3 i:18

[Aloop]rs1: 1 rs2: 19 rs3: 3 i:19

[A]rs1: 19 rs2: 19 rs3: 3

mut1 unlock for thread A

mut2 unlock for thread B

最佳答案

这两个线程试图锁定不同的互斥量,因此它们不可能死锁。

虽然有许多不同的方式可以想象线程死锁,但除非一个线程试图获取另一个线程持有的互斥量,否则不会发生互斥量的死锁。 (当然,其他事情也需要发生。如果发生这种情况,它只会等待另一个线程释放互斥量而不是死锁。)

关于c++ - 互斥锁是否正常工作?不能死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43285400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com