Skip to content

Instantly share code, notes, and snippets.

@bitshifter
Created July 19, 2016 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitshifter/71cabad4400ccb03bd1358a6fafead21 to your computer and use it in GitHub Desktop.
Save bitshifter/71cabad4400ccb03bd1358a6fafead21 to your computer and use it in GitHub Desktop.
#ifndef TASK_FENCE_HPP
#define TASK_FENCE_HPP
#include <condition_variable>
#include <mutex>
class TaskFence
{
int taskCount_;
std::condition_variable taskCV_;
std::mutex taskMutex_;
public:
TaskFence() : taskCount_(0) {}
~TaskFence()
{
wait();
}
void increment()
{
std::lock_guard<std::mutex> lock(taskMutex_);
++taskCount_;
}
void decrement()
{
{
std::lock_guard<std::mutex> lock(taskMutex_);
--taskCount_;
}
taskCV_.notify_all();
}
void wait()
{
std::unique_lock<std::mutex> lock(taskMutex_);
taskCV_.wait(lock, [this]{return taskCount_ == 0;});
}
};
#endif // TASK_FENCE_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment