Created
July 19, 2016 07:47
-
-
Save bitshifter/71cabad4400ccb03bd1358a6fafead21 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
#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