first commit
This commit is contained in:
39
utils/vp_semaphore.h
Executable file
39
utils/vp_semaphore.h
Executable file
@@ -0,0 +1,39 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
namespace vp_utils {
|
||||
// semaphore for queue/deque data structures in VideoPipe, used for producer-consumer pattern.
|
||||
// it blocks the consumer thread until data has come.
|
||||
class vp_semaphore
|
||||
{
|
||||
public:
|
||||
vp_semaphore() {
|
||||
count_ = 0;
|
||||
}
|
||||
|
||||
void signal() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
++count_;
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
void wait() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait(lock, [=] { return count_ > 0; });
|
||||
--count_;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
count_ = 0;
|
||||
cv_.notify_one();
|
||||
}
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
int count_;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user