reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofThreadChannel.h
Go to the documentation of this file.
1#pragma once
2
3#include <mutex>
4#include <queue>
5#include <condition_variable>
6
28template<typename T>
30public:
38 :closed(false){}
39
66 bool receive(T & sentValue){
67 std::unique_lock<std::mutex> lock(mutex);
68 if(closed){
69 return false;
70 }
71 while(queue.empty() && !closed){
72 condition.wait(lock);
73 }
74 if(!closed){
75 std::swap(sentValue,queue.front());
76 queue.pop();
77 return true;
78 }else{
79 return false;
80 }
81 }
82
113 bool tryReceive(T & sentValue){
114 std::unique_lock<std::mutex> lock(mutex);
115 if(closed){
116 return false;
117 }
118 if(!queue.empty()){
119 std::swap(sentValue,queue.front());
120 queue.pop();
121 return true;
122 }else{
123 return false;
124 }
125 }
126
159 bool tryReceive(T & sentValue, int64_t timeoutMs){
160 std::unique_lock<std::mutex> lock(mutex);
161 if(closed){
162 return false;
163 }
164 if(queue.empty()){
165 condition.wait_for(lock, std::chrono::milliseconds(timeoutMs));
166 if(queue.empty()) {
167 return false;
168 }
169 }
170
171 if(!closed){
172 std::swap(sentValue,queue.front());
173 queue.pop();
174 return true;
175 }else{
176 return false;
177 }
178 }
179
206 bool send(const T & value){
207 std::unique_lock<std::mutex> lock(mutex);
208 if(closed){
209 return false;
210 }
211 queue.push(value);
212 condition.notify_one();
213 return true;
214 }
215
247 bool send(T && value){
248 std::unique_lock<std::mutex> lock(mutex);
249 if(closed){
250 return false;
251 }
252 queue.push(std::move(value));
253 condition.notify_one();
254 return true;
255 }
256
263 void close(){
264 std::unique_lock<std::mutex> lock(mutex);
265 closed = true;
266 condition.notify_all();
267 }
268
269
275 bool empty() const{
276 return queue.empty();
277 }
278
279
284 size_t size() const {
285 return queue.size();
286 }
287
288private:
290 std::queue<T> queue;
291
293 std::mutex mutex;
294
296 std::condition_variable condition;
297
299 bool closed;
300
301};
Safely send data between threads without additional synchronization.
Definition ofThreadChannel.h:29
size_t size() const
Queries size of queue.
Definition ofThreadChannel.h:284
bool empty() const
Queries empty channel.
Definition ofThreadChannel.h:275
bool tryReceive(T &sentValue)
If available, receive a new sent value without blocking.
Definition ofThreadChannel.h:113
bool tryReceive(T &sentValue, int64_t timeoutMs)
If available, receive a new sent value or wait for a user-specified duration.
Definition ofThreadChannel.h:159
void close()
Close the ofThreadChannel.
Definition ofThreadChannel.h:263
bool receive(T &sentValue)
Block the receiving thread until a new sent value is available.
Definition ofThreadChannel.h:66
ofThreadChannel()
Create a default ofThreadChannel.
Definition ofThreadChannel.h:37
bool send(T &&value)
Send a value to the receiver without making a copy.
Definition ofThreadChannel.h:247
bool send(const T &value)
Send a value to the receiver by making a copy.
Definition ofThreadChannel.h:206