-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cpp
56 lines (47 loc) · 838 Bytes
/
Worker.cpp
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
#include "Worker.hpp"
#include "WorkerManager.hpp"
#include "IWork.hpp"
Worker::Worker(WorkerManager *workerManager)
{
work_ = 0;
workerManager_ = workerManager;
keepOn = true;
}
Worker::~Worker()
{
delete this->work_;
keepOn = false;
::pthread_join(pthread_, 0);
}
void Worker::_setWork(IWork *work)
{
delete work_;
work_ = work;
}
void Worker::getWork()
{
_setWork(workerManager_->getWork());
}
void Worker::doWork()
{
work_->doWork(workerManager_);
}
void Worker::run()
{
::pthread_create(&pthread_, 0, _curcle, this);
}
void *Worker::_curcle(void *arg)
{
Worker *self = static_cast<Worker *>(arg);
while (self->isKeepOn()) {
self->getWork();
self->workerManager_->activeWorkers_++;
self->doWork();
self->workerManager_->activeWorkers_--;
}
return 0;
}
bool Worker::isKeepOn() const
{
return keepOn;
}