Created
March 7, 2012 13:07
-
-
Save lcapaldo/1993005 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 resource { | |
recursive_mutex m_; | |
condition_variable cv_; | |
public: | |
resource() {} | |
void with_do(std::function<void()> body) | |
{ | |
m_.lock(); | |
at_scope_exit u( [&]() { cv_.notify_all(); m_.unlock(); } ); | |
body(); | |
} | |
void with_when_do(std::function<bool()> cond, std::function<void()> body) | |
{ | |
with_do( [&]() { | |
while( !cond() ) | |
{ | |
cv_.wait(m_); | |
} | |
body(); }); | |
} | |
void await(std::function<bool()> cond) | |
{ | |
while(!cond()) | |
{ | |
cv_.wait(m_); | |
} | |
} | |
private: | |
resource(const resource&); | |
resource& operator=(resource); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment