-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSharedQueue.hpp
153 lines (133 loc) · 2.29 KB
/
SharedQueue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <list>
#include <mutex>
#include <condition_variable>
#if 1 == 1 //__cplusplus >= 199711L
template <typename T> class SharedQueue
{
std::list<T> m_list;
std::mutex m_mutex;
std::condition_variable m_cond;
public:
SharedQueue()
{
}
~SharedQueue()
{
}
void add(T item)
{
m_mutex.lock();
m_list.emplace_back(item);
m_mutex.unlock();
m_cond.notify_one();
}
T remove()
{
std::unique_lock<std::mutex> lk(m_mutex);
while (m_list.size() == 0)
{
m_cond.wait(lk);
}
T item = m_list.front();
m_list.pop_front();
return item;
}
size_t isThereaWork()
{
size_t uSize = 0;
m_mutex.lock();
uSize = m_list.size();
m_mutex.unlock();
return uSize;
}
T soIgetIt()
{
T oItem;
m_mutex.lock();
oItem = m_list.front();
m_list.pop_front();
m_mutex.unlock();
return oItem;
}
size_t size()
{
size_t uSize = 0;
m_mutex.lock();
uSize = m_list.size();
m_mutex.unlock();
return uSize;
}
};
#else
#ifdef _WIN32
#error Older version of Windows mutex is not implemented
#elif __linux__
#include <pthread.h>
template <typename T> class SharedQueue
{
std::list<T> m_list;
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
public:
SharedQueue()
{
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
}
~SharedQueue()
{
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
void add(T item)
{
pthread_mutex_lock(&m_mutex);
m_list.push_back(item);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
}
T remove()
{
pthread_mutex_lock(&m_mutex);
while (m_list.size() == 0)
{
pthread_cond_wait(&m_cond, &m_mutex);
}
T item = m_list.front();
m_list.pop_front();
pthread_mutex_unlock(&m_mutex);
return item;
}
int isThereaWork()
{
pthread_mutex_lock(&m_mutex);
int size = m_list.size();
if (size > 0) return size;
else
{
//unlocking for next questioning thread
pthread_mutex_unlock(&m_mutex);
return size;
}
}
T soIgetIt()
{
T item = m_list.front();
m_list.pop_front();
pthread_mutex_unlock(&m_mutex);
return item;
}
int size()
{
pthread_mutex_lock(&m_mutex);
int size = m_list.size();
pthread_mutex_unlock(&m_mutex);
return size;
}
};
#else
#error Not supported platform for older C++, Only Linux or Windows!!
#endif
//
#endif