-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueworkers.cpp
More file actions
51 lines (44 loc) · 1.04 KB
/
queueworkers.cpp
File metadata and controls
51 lines (44 loc) · 1.04 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
#include "wq/queueworkers.h"
namespace wq {
work_queue_s::workers_s::workers_s(work_queue_p _p) : p_queue(_p) {
::pthread_attr_init(&about_worker);
}
work_queue_s::workers_s::~workers_s() {
::pthread_attr_destroy(&about_worker);
}
void* work_queue_s::workers_s::worker_run(void* _q) {
static unsigned n_workers = 0;
unsigned i_worker = ++n_workers;
unsigned n_idle = 0;
unsigned n_work = 0;
work_queue_p p_queue = (work_queue_p) _q;
while (p_queue->q_live) {
function_t work;
if (p_queue->dequeue_work(work)) {
work();
++n_work;
} else {
++n_idle;
}
}
printf("[Worker %3u of %3u] %9u idle %9u work\n", i_worker, n_workers, n_idle, n_work);
return 0;
}
bool work_queue_s::workers_s::worker_start() {
pthread_t id_thread;
int v = ::pthread_create(&id_thread, &about_worker, worker_run, p_queue);
if (v) {
return false;
}
::pthread_detach(id_thread);
return true;
}
bool work_queue_s::workers_s::worker_start(unsigned _n) {
for (auto i = 0; i < _n; ++i) {
if (!worker_start()) {
return false;
}
}
return true;
}
}