Skip to content

Instantly share code, notes, and snippets.

@lcapaldo
Created March 7, 2012 13:07
Show Gist options
  • Save lcapaldo/1993005 to your computer and use it in GitHub Desktop.
Save lcapaldo/1993005 to your computer and use it in GitHub Desktop.
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