Created
January 9, 2020 06:38
-
-
Save arcticmatt/ae700c7da059123962d7960447c45349 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
class CustomScopedLock { | |
public: | |
CustomScopedLock(std::mutex &m) : m_(m) { | |
std::cout << "CustomScopedLock locking mutex..." << std::endl; | |
m_.lock(); | |
std::cout << "CustomScopedLock has locked mutex!" << std::endl; | |
}; | |
~CustomScopedLock() { | |
m_.unlock(); | |
std::cout << "CustomScopedLock has unlocked mutex" << std::endl; | |
}; | |
private: | |
std::mutex &m_; | |
}; | |
void lockMutexCustomScopeLock(bool shouldThrow) { | |
CustomScopedLock lock(globalMutex); | |
if (shouldThrow) { | |
std::cout << "Throwing exception" << std::endl; | |
throw 1; | |
} | |
} | |
void lockMutexCustomScopeLock() { | |
try { | |
lockMutexCustomScopeLock(true); | |
} catch (...) { | |
std::cout << "Caught exception" << std::endl; | |
} | |
lockMutexCustomScopeLock(false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment