fastcat 0.13.15
C++ EtherCAT Device Command & Control Library
Loading...
Searching...
No Matches
thread_safe_queue.h
Go to the documentation of this file.
1#ifndef FASTCAT_THREAD_SAFE_QUEUE_H_
2#define FASTCAT_THREAD_SAFE_QUEUE_H_
3
4#include <queue>
5#include <mutex>
6
7namespace fastcat {
8template <class T>
10{
11public:
13
15
16 void push(T t)
17 {
18 std::lock_guard<std::mutex> lock(m);
19 q.push(t);
20 }
21
22 T front(void)
23 {
24 std::lock_guard<std::mutex> lock(m);
25 return q.front();
26 }
27
28 void pop(void)
29 {
30 std::lock_guard<std::mutex> lock(m);
31 q.pop();
32 }
33
34 bool empty(void)
35 {
36 std::lock_guard<std::mutex> lock(m);
37 return q.empty();
38 }
39
40 bool try_pop(T& item)
41 {
42 std::lock_guard<std::mutex> lock(m);
43 if (q.empty()) {
44 return false;
45 }
46 item = q.front();
47 q.pop();
48 return true;
49 }
50
51
52private:
53 std::queue<T> q;
54 mutable std::mutex m;
55
56};
57
58}
59#endif
Definition thread_safe_queue.h:10
bool try_pop(T &item)
Definition thread_safe_queue.h:40
T front(void)
Definition thread_safe_queue.h:22
void push(T t)
Definition thread_safe_queue.h:16
bool empty(void)
Definition thread_safe_queue.h:34
~ThreadSafeQueue(void)
Definition thread_safe_queue.h:14
ThreadSafeQueue(void)
Definition thread_safe_queue.h:12
void pop(void)
Definition thread_safe_queue.h:28
Definition device_base.h:18