-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy paththreadSamples-part3.cpp
More file actions
186 lines (180 loc) · 4.42 KB
/
threadSamples-part3.cpp
File metadata and controls
186 lines (180 loc) · 4.42 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
#include <thread>
#include <iostream>
#include <queue>
#include <functional>
#include <fstream>
using namespace std;
template <typename T>
class MutexSafe
{
private:
mutex _mutex;
T* _resource;
T* operator ->(){}
T& operator &(){}
public:
MutexSafe(T* resource):_resource(resource){}
~MutexSafe(){delete _resource;}
void lock()
{
_mutex.lock();
}
void unlock()
{
_mutex.unlock();
}
bool try_lock()
{
return _mutex.try_lock();
}
mutex& Mutex()
{
return _mutex;
}
T& Acquire (unique_lock<MutexSafe<T>>& lock)
{
MutexSafe<T> *_safe = lock.mutex();
if(&_safe->Mutex()!=&_mutex)
{
throw "wrong lock object passed to Acquire function.\n";
}
return *_resource;
}
T& Acquire (unique_lock<mutex>& lock)
{
if(lock.mutex()!=&_mutex)
{
throw "wrong lock object passed to Acquire function.\n";
}
return *_resource;
}
};
template <typename MsgType>
class MsgQueue
{
private:
queue<MsgType> _queue;
mutex _mutex;
condition_variable _enqCv;
condition_variable _deqCv;
int _limit;
public:
MsgQueue(int limit=3):_limit(limit){}
void Enqueue(MsgType & msg)
{
unique_lock<mutex> lock(_mutex);
if(_queue.size()>=_limit)
{
//cout<<"queue is full, wait()..."<<endl;
_enqCv.wait(lock,[this]{return _queue.size()<_limit;});
}
_queue.push(msg);
_deqCv.notify_one();
}
MsgType Dequeue()
{
unique_lock<mutex> lock(_mutex);
if(_queue.size()<=0)
{
cout<<"queue is empty, wait()..."<<endl;
_deqCv.wait(lock, [this]{return _queue.size()>0;});
}
MsgType& msg = _queue.front();
_queue.pop();
_enqCv.notify_one();
return msg;
}
int Size()
{
unique_lock<mutex> lock(_mutex);
return _queue.size();
}
};
struct StockCommand
{
string command;
float price;
StockCommand(){}
StockCommand(const StockCommand& cp):command(cp.command),price(cp.price){}
void ExecuteCommand()
{
if(price>0)
cout<<"Command "<<command<<" is executed at price $"<<price<<endl;
else
cout<<"market closed because the price is $"<<price<<endl;
}
};
typedef MsgQueue<StockCommand> TaskQueueType;
typedef MutexSafe<TaskQueueType> TaskQueueSafe;
class ThreadPool
{
private:
int _limit;
vector<thread*> _workerThreads;
TaskQueueType& _taskQueue;
bool _threadPoolStop=false;
public:
void ExecuteCommand()
{
while(1)
{
StockCommand task=_taskQueue.Dequeue();
task.ExecuteCommand();
if(task.price<0)
{
_threadPoolStop=true;
_taskQueue.Enqueue(task);//tell other threads to stop
}
if(_threadPoolStop)
{
cout<< "thread finshed!"<<endl;
return;
}
//the sleep function simulates that the task takes a while to finish
std::this_thread::sleep_for(std::chrono::milliseconds(rand()%100));
}
}
ThreadPool(TaskQueueType& taskQueue,int limit=3):_limit(limit),_taskQueue(taskQueue){
for(int i=0;i<_limit;++i)
{
_workerThreads.push_back(new thread(&ThreadPool::ExecuteCommand,this));
}
}
~ThreadPool(){
for(auto threadObj: _workerThreads)
{
if(threadObj->joinable())
{
threadObj->join();
delete threadObj;
}
}
}
};
void TestThreadPool()
{
TaskQueueType taskQueue(5);
ThreadPool pool(taskQueue,3);
//the sleep function simulates the situation that all
//worker threads are waiting for the empty message queue at the beginning
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
for(int i=0;i<10;i++)
{
StockCommand task;
task.price= i+1;
if(task.price >5)
task.command = "sell at price $";
else
task.command = "buy at price $";
taskQueue.Enqueue(task);
}
StockCommand marketCloseCommand;
marketCloseCommand.command="Market Closed!";
marketCloseCommand.price=-1;
taskQueue.Enqueue(marketCloseCommand);
}
int main()
{
TestThreadPool();
return 0;
}