You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.2 KiB
C
81 lines
1.2 KiB
C
2 years ago
|
#pragma once
|
||
|
#include<mutex>
|
||
|
//#include<condition_variable>
|
||
|
#include<list>
|
||
|
#include<iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
template<class T> class ASyncQueue
|
||
|
{
|
||
|
public:
|
||
|
ASyncQueue(int max_size);
|
||
|
~ASyncQueue();
|
||
|
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<T> q;
|
||
|
int size;
|
||
|
int max_size;
|
||
|
};
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
template<class T>
|
||
|
ASyncQueue<T>::ASyncQueue(int max_size) :max_size(max_size)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
ASyncQueue<T>::~ASyncQueue()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
void ASyncQueue<T>::put(const T& val)
|
||
|
{
|
||
|
lock_guard<mutex> locker(lock);
|
||
|
q.emplace_back(val);
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
void ASyncQueue<T>::take(T& val)
|
||
|
{
|
||
|
lock_guard<mutex> locker(lock);
|
||
|
val = q.front();
|
||
|
q.pop_front();
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
void ASyncQueue<T>::clear()
|
||
|
{
|
||
|
lock_guard<mutex> locker(lock);
|
||
|
q.clear();
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
bool ASyncQueue<T>::isEmpty()
|
||
|
{
|
||
|
return q.size() == 0;
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
bool ASyncQueue<T>::isFull()
|
||
|
{
|
||
|
return q.size() == max_size;
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
int ASyncQueue<T>::count()
|
||
|
{
|
||
|
lock_guard<mutex> locker(lock);
|
||
|
return q.size();
|
||
|
}
|