21 #ifndef GRPPI_COMMON_MPMC_QUEUE_H 22 #define GRPPI_COMMON_MPMC_QUEUE_H 29 #include <condition_variable> 44 size{q_size}, buffer{std::vector<T>(q_size)}, mode{q_mode}, pread{0}, pwrite{0}, internal_pread{0}, internal_pwrite{0} { }
48 buffer{std::move(q.buffer)},
50 pread{q.pread.load()},
51 pwrite{q.pwrite.load()},
52 internal_pread{q.internal_pread.load()},
53 internal_pwrite{q.internal_pwrite.load()},
62 bool is_empty ()
const noexcept;
67 bool is_full (
unsigned long long current)
const noexcept;
68 bool is_empty (
unsigned long long current)
const noexcept;
71 std::vector<T> buffer;
74 std::atomic<unsigned long long> pread;
75 std::atomic<unsigned long long> pwrite;
76 std::atomic<unsigned long long> internal_pread;
77 std::atomic<unsigned long long> internal_pwrite;
81 std::condition_variable empty;
82 std::condition_variable full;
89 return pread.load()==pwrite.load();
96 unsigned long long current;
99 current = internal_pread.load();
100 }
while(!internal_pread.compare_exchange_weak(current, current+1));
102 while(is_empty(current));
104 auto item = std::move(buffer[current%size]);
108 }
while(!pread.compare_exchange_weak(current, current+1));
110 return std::move(item);
113 std::unique_lock<std::mutex> lk(m);
114 while(is_empty(pread)){
117 auto item = std::move(buffer[pread%size]);
122 return std::move(item);
127 template <
typename T>
130 unsigned long long current;
132 current = internal_pwrite.load();
133 }
while(!internal_pwrite.compare_exchange_weak(current, current+1));
135 while(is_full(current));
137 buffer[current%size] = std::move(item);
142 }
while(!pwrite.compare_exchange_weak(current, current+1));
147 std::unique_lock<std::mutex> lk(m);
148 while(is_full(pwrite)){
151 buffer[pwrite%size] = std::move(item);
161 template <
typename T>
163 if(current >= pwrite.load())
return true;
167 template <
typename T>
169 if(current >= (pread.load()+size))
return true;
176 template <
typename T>
179 template <
typename T>
184 template <
typename T>
187 template <
typename T>
Definition: callable_traits.h:26
bool is_empty() const noexcept
Definition: mpmc_queue.h:88
mpmc_queue(mpmc_queue &&q)
Definition: mpmc_queue.h:46
constexpr bool is_queue
Definition: mpmc_queue.h:185
queue_mode
Definition: mpmc_queue.h:35
Definition: mpmc_queue.h:38
std::enable_if_t< is_queue< T >, int > requires_queue
Definition: mpmc_queue.h:188
T value_type
Definition: mpmc_queue.h:41
T pop()
Definition: mpmc_queue.h:93
Definition: mpmc_queue.h:177
bool push(T item)
Definition: mpmc_queue.h:128