Skip to content

Instantly share code, notes, and snippets.

@arcticmatt
Created January 9, 2020 06:34
Show Gist options
  • Save arcticmatt/2bd15a91ea295314d2e34ec9414e2e28 to your computer and use it in GitHub Desktop.
Save arcticmatt/2bd15a91ea295314d2e34ec9414e2e28 to your computer and use it in GitHub Desktop.
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