Skip to content

Instantly share code, notes, and snippets.

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