UDPConnection/cpp_impl/src/TSQueue.hpp

41 lines
940 B
C++
Raw Normal View History

2019-06-06 07:40:04 +00:00
#ifndef UDPC_THREADSAFE_QUEUE_HPP
#define UDPC_THREADSAFE_QUEUE_HPP
#define UDPC_TSQUEUE_DEFAULT_CAPACITY 32
#include <atomic>
#include <cstdlib>
#include <memory>
class TSQueue {
public:
TSQueue(unsigned int elemSize,
unsigned int capacity = UDPC_TSQUEUE_DEFAULT_CAPACITY);
~TSQueue();
// disable copy
TSQueue(const TSQueue &other) = delete;
TSQueue &operator=(const TSQueue &other) = delete;
// disable move
TSQueue(TSQueue &&other) = delete;
TSQueue &operator=(TSQueue &&other) = delete;
bool push(void *data);
std::unique_ptr<unsigned char[]> top();
bool pop();
void clear();
void changeCapacity(unsigned int newCapacity);
unsigned int size();
2019-06-06 07:40:04 +00:00
private:
unsigned int elemSize;
unsigned int capacity;
unsigned int head;
unsigned int tail;
bool isEmpty;
std::unique_ptr<unsigned char[]> buffer;
std::atomic_bool spinLock;
};
#endif