-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Debug Develop
committed
Apr 20, 2020
1 parent
d47015b
commit bf7ee52
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} |