#pragma once #include #include #include #include using namespace std; template class SyncQueue { public: SyncQueue(int max_size); ~SyncQueue(); void put(const T& val); void take(T& val); void clear(); bool isEmpty(); bool isFull(); int count(); public: string name; private: mutex lock; condition_variable_any cv_full, cv_empty; list q; int size; int max_size; }; using namespace std; template SyncQueue::SyncQueue(int max_size) :max_size(max_size) { } template SyncQueue::~SyncQueue() { } template void SyncQueue::put(const T& val) { lock_guard locker(lock); while (isFull()) { //cout << "\nQueue " << name << " is full now, wait a minute!" << endl; cv_full.wait(lock); } q.emplace_back(val); cv_empty.notify_one(); //cout << " Put image success " << endl; } template void SyncQueue::take(T& val) { lock_guard locker(lock); while (isEmpty()) { //cout << "\nQueue "<< name << " is empty now, wait a minute!" << endl; cv_empty.wait(lock); } val = q.front(); q.pop_front(); cv_full.notify_one(); //cout << " Take image success "<< endl; } template void SyncQueue::clear() { lock_guard locker(lock); q.clear(); } template bool SyncQueue::isEmpty() { return q.size() == 0; } template bool SyncQueue::isFull() { return q.size() == max_size; } template int SyncQueue::count() { lock_guard locker(lock); return q.size(); }