Created
April 7, 2017 11:31
-
-
Save ivcn/227414b3185840434718f7f6c8cbffb1 to your computer and use it in GitHub Desktop.
Simple deadlock example in C++
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
| int main() { | |
| std::mutex m1; | |
| std::mutex m2; | |
| std::thread t1([&m1, &m2] { | |
| print("1. Acquiring m1."); | |
| m1.lock(); | |
| std::this_thread::sleep_for(std::chrono::milliseconds(10)); | |
| print("1. Acquiring m2"); | |
| m2.lock(); | |
| }); | |
| std::thread t2([&m1, &m2] { | |
| print("2. Acquiring m2"); | |
| m2.lock(); | |
| std::this_thread::sleep_for(std::chrono::milliseconds(10)); | |
| print("2. Acquiring m1"); | |
| m1.lock(); | |
| }); | |
| t1.join(); | |
| t2.join(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment