-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbounded.buffer.cpp
More file actions
191 lines (155 loc) · 3.84 KB
/
bounded.buffer.cpp
File metadata and controls
191 lines (155 loc) · 3.84 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <boost/circular_buffer.hpp>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <vector>
#include <iostream>
const unsigned long QUEUE_SIZE = 1000L;
using namespace std;
mutex io_mutex;
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
template <class T>
class BoundedBuffer
{
public:
typedef boost::circular_buffer<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
explicit BoundedBuffer(size_type capacity)
: m_unread(0), m_container(capacity)
{}
BoundedBuffer(const BoundedBuffer&) = delete;
auto count() const { return m_unread; }
auto push_front(const value_type& item) -> bool
{
unique_lock lock(m_mutex);
// 집어넣으려면 공간이 생길때까지 기다려야 한다.
m_while_full.wait(lock,
[this] { return m_unread < m_container.capacity() || m_shutdown; });
if (m_shutdown)
return false;
m_container.push_front(item);
++m_unread;
lock.unlock();
m_while_empty.notify_all();
return true;
}
auto pop_back(value_type* pItem) -> bool
{
unique_lock lock(m_mutex);
// 빼내려면 하나이상 존재햐야 한다.
cout << "comsumer waiting ..\n";
m_while_empty.wait(lock, [this] { return m_unread > 0 || m_shutdown; });
if (m_shutdown)
return false;
*pItem = m_container[--m_unread];
lock.unlock();
m_while_full.notify_all();
return true;
}
void shutdown()
{
m_shutdown = true;
m_while_full.notify_all();
m_while_empty.notify_all();
}
private:
bool m_shutdown {false};
size_type m_unread;
container_type m_container;
mutex m_mutex;
condition_variable m_while_empty;
condition_variable m_while_full;
};
template <typename Buffer>
class Consumer
{
public:
Consumer(Buffer* buffer)
: m_container(buffer)
{
}
void operator()()
{
while (true)
{
auto res = m_container->pop_back(&m_item);
if (!res)
{
cout << "shutdown consumer\n";
break;
}
{ // RAII idom: Resource Acquisition Is Initialization idiom
unique_lock l(io_mutex);
cout << "consumer: " << m_item << " count: " << m_container->count() << "\n";
}
}
}
private:
typedef typename Buffer::value_type value_type;
Buffer* m_container;
value_type m_item;
};
template<class Buffer>
class Producer
{
public:
Producer(Buffer* buffer, int init)
: m_container(buffer), m_init(init)
{}
void operator()()
{
/* TODO
1) 쉬면서 데이타를 넣는다.
2) 적당한 시점에 신호를 받고 종료한다.
*/
for (int i=m_init; ; ++i)
{
auto res = m_container->push_front(int(i));
if (!res)
{
cout << "shutdown Producer\n";
break;
}
{
unique_lock l(io_mutex);
cout << "producer: " << i << " count: " << m_container->count() << "\n";
}
this_thread::sleep_for(1s);
}
}
private:
typedef typename Buffer::value_type value_type;
Buffer* m_container;
int m_init;
};
template<class Buffer>
void fifo_test(Buffer* buffer)
{
// 1. prepare buffer
for (int i=0; i<100; i++) buffer->push_front(i);
// 2. prepare producers
vector<thread> pool;
Consumer<Buffer> consumer(buffer);
thread consume(consumer);
for (int i=0; i<10; i++)
pool.emplace_back(Producer<Buffer>(buffer, i*100));
this_thread::sleep_for(10s);
// x. interrupts thread
cout << "shutdwon from main thread\n";
buffer->shutdown();
// x. Joint the threads
for (auto& t : pool)
if (t.joinable()) t.join();
consume.join();
}
int main(int /*argc*/, char* /*argv*/[])
{
BoundedBuffer<int> bb_int(QUEUE_SIZE);
fifo_test(&bb_int);
return 0;
}