Created
January 9, 2020 06:34
-
-
Save arcticmatt/2bd15a91ea295314d2e34ec9414e2e28 to your computer and use it in GitHub Desktop.
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
void lockMutexBad(bool shouldThrow) { | |
std::cout << "Locking mutex manually..." << std::endl; | |
globalMutex.lock(); | |
std::cout << "Mutex is locked!" << std::endl; | |
// Could also imagine this as an early return. If you use a plain old | |
// mutex, you have to remember to manually unlock before every return... | |
// and it's quite easy to forget. | |
if (shouldThrow) { | |
std::cout << "Throwing exception" << std::endl; | |
throw 1; | |
} | |
globalMutex.unlock(); | |
std::cout << "Mutex has been unlocked manually" << std::endl; | |
} | |
void lockMutexBadExample() { | |
try { | |
lockMutexBad(true); | |
} catch (...) { | |
std::cout << "Caught exception" << std::endl; | |
} | |
lockMutexBad(false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment