Skip to content

Commit

Permalink
Deadlock simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Debug Develop committed Apr 20, 2020
1 parent d47015b commit bf7ee52
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Semaphore/Deadlock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//this code will simulate deadlock condition
//compile with -std=c++0x -pthread
#include <iostream>
#include <thread>
using namespace std;

bool stop=1;
//pointer to thread
thread* func1;
thread* func2;

void function1(bool foo){
while(stop){;}
::func2->join();
}

void function2(bool foo){
while(stop){;}
::func1->join();
}

int main(){
//create thread with process inside it
thread thread1(function1, 1);
thread thread2(function2, 1);

::func1=&thread1;
::func2=&thread2;
::stop=0;
thread1.join();
thread2.join();
//if the text below is printed, there is no deadlock
cout << "Hello Home!";
}

0 comments on commit bf7ee52

Please sign in to comment.