c++ - Is mutex locks note working properly? Can't Deadlock -


so i'm trying make c++ program should deadlock. following code believe should work doesn't deadlock. either mutex.lock()'s not working properly? mean if mut2.lock() locks critical section should unlock until it's done right meaning funca shouldn't run or @ least wait till mut2.unlock()'s since both use resources rs2? why can't deadlock program?

#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 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 thread a\n"; } void funcb(){      mut2.lock();     cout<<"mut2 lock thread b\n";     rs3 = rs1 + rs2;     cout<<"[b]rs3: "<<rs3 << " rs1: "<<rs1 << " rs2: "<<rs2 <<endl;     sleep(50);     mut2.unlock();       cout<<"mut2 unlock thread b\n"; }   int main(){     thread ta(funca);     thread tb(funcb);      ta.join();     tb.join();  return 0; }  //here 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 output 

mut2 lock thread b

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

mut1 lock 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 thread a

mut2 unlock thread b

the 2 threads attempt lock different mutexes, there's no way can deadlock.

while there many different ways 1 can imagine threads deadlocking, deadlock mutexes cannot occur unless 1 thread tries acquire mutex other thread holds. (of course, other things need happen too. if happens, wait other thread release mutex rather deadlocking.)


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -