wangrong
2025-01-23 02819b5c047bb354b0ef2374e7c6a6ac4bbd5bba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
#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_;
        }
 
    private:
        std::mutex mutex_;
        std::condition_variable cv_;
        int count_;
    };
}