-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (159 loc) · 4.34 KB
/
main.cpp
File metadata and controls
225 lines (159 loc) · 4.34 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "threadpool.h"
using namespace std;
#define BUFFER_SIZE 1024
ThreadPool *myPool = NULL;
string callee( char* name ){
string result;
srandom(time(NULL));
int rdm = random() % 8;
switch ( rdm ){
case 1:
case 2:
case 3:
case 4:
case 5: {
cout << "我是 short task" << endl;
sleep( 1 );
} break;
case 6:
case 7: {
cout << "我是 long task" << endl;
sleep( 7 );
} break;
default: {
cout << "我是 tiny task" << endl;
} break;
}
result = result + name + " is return by callee.";
return result;
}
class SampleWorkerThread : public WorkerThread
{
public:
int id;
string _data;
int _pipefd_return;
unsigned virtual executeThis()
{
char *param = const_cast<char*>(_data.c_str());
string result = callee( param );
char buf2[BUFFER_SIZE];
strcpy( buf2, result.c_str() );
// 將 callee 的回傳值,透過 pipe 寫回給 caller.
write( _pipefd_return, &buf2, strlen( result.c_str() ) );
return(0);
}
SampleWorkerThread(int id, int pipefd_return, string data ) : WorkerThread(id), id(id)
{
_pipefd_return = pipefd_return;
_data = data;
cout << " Creating SampleWorkerThread " << id << "\t address=" << this << endl;
}
~SampleWorkerThread()
{
cout << " Deleting SampleWorkerThread " << id << "\t address=" << this << endl;
}
};
string caller ( const char * name ){
int pipefd[2];
if (pipe(pipefd) == -1) {
cout << "Pipe initialize error" << endl;
return NULL;
}
SampleWorkerThread* wthr = new SampleWorkerThread( 1, pipefd[1], name );
// 丟給 thread pool 執行
//myPool->assignWork( wthr ); // API: change
myPool->assign_work( wthr );
fd_set rfds;
struct timeval tv;
int retval;
int fd_max;
/* Watch pipefd[1] to see when it has input. */
FD_ZERO(&rfds);
FD_SET( pipefd[0], &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 10;
tv.tv_usec = 0;
fd_max = pipefd[1]+1;
// 用 select 等待回傳
retval = select( fd_max, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if( retval > 0 ){
cout << "回傳觸發了!!" << endl;
/* FD_ISSET(0, &rfds) will be true. */
// 處理回傳值
char buf2[BUFFER_SIZE];
read( pipefd[0], &buf2, BUFFER_SIZE ); // 接 callee 回傳的資料
cout << " buffer :: " << buf2 << endl;
string result = string( buf2 );
return result;
}
else if ( retval == 0 ){
cout << "等不到回傳" << endl;
// 處理逾期
}
else {
cout << "等待有錯誤" << endl;
// 處理例外
}
return NULL;
}
static void * thread_start(void *arg)
{
string result = caller( "DATA_002" );
cout << "Noreturn Call result: " << result << endl;
return NULL;
}
int main(int argc, char **argv)
{
//myPool = new ThreadPool(1); // single thread;
myPool = new ThreadPool(5); // multi thread;
//myPool->initializeThreads(); // API: remove
//We will count time elapsed after initializeThreads()
time_t t1=time(NULL);
int task_num= 100;
pthread_t thread_id[task_num];
for ( int i = 0; i< task_num; i++ ){
srandom(time(NULL));
int rdm = random() % 3;
switch ( rdm ){
case 0: {
cout << "Blocking Calling" << endl;
string result = caller( "DATA_001" );
cout << "Blocking Call return: " << result << endl;
} break;
case 1: {
cout << "Non-blocking Calling ( not yet implemented)" << endl;
sleep (1);
} break;
case 2: {
cout << "Noreturn Calling" << endl;
pthread_create( &thread_id[i], NULL, &thread_start,NULL);
sleep (1);
} break;
default: {
} break;
}
}
time_t t2=time(NULL);
// 注意,切換不同的 Worker thread 數,可以看到時間上的差異
cout << t2-t1 << " seconds elapsed\n" << endl;
// destroyPool(int maxPollSecs)
// Before actually destroying the ThreadPool, this function checks if all the pending work is completed.
// If the work is still not done, then it will check again after maxPollSecs
// The default value for maxPollSecs is 2 seconds.
// And ofcourse the user is supposed to adjust it for his needs.
//myPool->destroyPool(2); // API: remove
sleep( 3 ); // 若 destructor 是強制 kill, 需設 sleep 以避免太早被關閉
delete myPool;
return 0;
}