Created
February 21, 2018 12:24
-
-
Save Geal/baabe9fb81dc52f63d232dc6328ca4c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
#include <thread> | |
#include <mutex> | |
#include <chrono> | |
std::mutex a_lock; | |
std::mutex b_lock; | |
void philosophe() | |
{ | |
std::cout << "entered thread 1" << std::endl; | |
while(true) { | |
a_lock.lock(); | |
b_lock.lock(); | |
std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 10)); | |
std::cout << "1" << std::endl; | |
b_lock.unlock(); | |
a_lock.unlock(); | |
} | |
} | |
int main() { | |
srand((unsigned int)time(0)); | |
int a = 0; | |
std::thread t1(func1); | |
std::thread t2(func2); | |
t1.join(); | |
t2.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment