Skip to content

Instantly share code, notes, and snippets.

@Geal
Created February 21, 2018 12:24
Show Gist options
  • Save Geal/baabe9fb81dc52f63d232dc6328ca4c4 to your computer and use it in GitHub Desktop.
Save Geal/baabe9fb81dc52f63d232dc6328ca4c4 to your computer and use it in GitHub Desktop.
#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