Created
February 15, 2017 23:05
-
-
Save Chippiewill/a84ad7d744ad2ebb3f927144a5639d76 to your computer and use it in GitHub Desktop.
unique_lock_ref
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
#include <iostream> | |
#include <mutex> | |
template <typename mutex> | |
struct unique_lock_ref { | |
unique_lock_ref(const std::unique_lock<mutex>& lh) { | |
if (!lh) { | |
throw std::logic_error("Cannot instantiate a unique_lock_ref " | |
"with an unlocked unique_lock"); | |
} | |
} | |
}; | |
float my_locked_func(unique_lock_ref<std::mutex> r, int some_other_arg, float another_arg) { | |
return some_other_arg + another_arg; | |
} | |
std::mutex m; | |
int main(int argc, char** argv) { | |
std::unique_lock<std::mutex> lh(m); | |
my_locked_func(lh, 3, 5.6); | |
lh.unlock(); | |
my_locked_func(lh, 4, 5.2); // throws logic_error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment