-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.cpp
More file actions
executable file
·90 lines (74 loc) · 1.79 KB
/
worker.cpp
File metadata and controls
executable file
·90 lines (74 loc) · 1.79 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
#include "worker.h"
// #include <QTimer>
#include <iostream>
std::atomic<int> Worker::countWorkers{0};
//public:
Worker::Worker(QObject *parent)
: QThread(parent), workerId(++countWorkers), running(false), status("Ожидает")
{
}
void Worker::assignTask(ITask* task)
{
std::lock_guard<std::mutex> lock(taskMutex);
this->task = task;
taskId = task->getId();
running = true;
status = "Выполняет задачу №" + std::to_string(task->getId());
emit changeStatus(workerId);
taskCondition.notify_one();
}
bool Worker::isRun() const
{
return running;
}
std::string Worker::getStatus() const
{
return status;
}
int Worker::getId() const
{
return workerId;
}
int Worker::getTaskId() const
{
return taskId;
}
void Worker::stop()
{
running = false;
status = "Ожидает";
emit changeStatus(workerId);
taskCondition.notify_one();
terminate();
}
void Worker::stopTask()
{
running = false;
status = "Ожидает";
emit changeStatus(workerId);
taskCondition.notify_one();
}
//protected:
void Worker::run()
{
std::unique_lock<std::mutex> lock(taskMutex);
while (true) {
taskCondition.wait(lock, [this]() { return task != nullptr && running; });
if(task && running)
while (!task->isCompleted() && running) {
task->executeStep();
QThread::msleep(50);
if (task->flagDelete){
task->taskFinished(taskId);
break;
}
}
running = false;
QThread::sleep(5);
if(task)
task->deleteTask(); //TODO тут сигнал на удаление таски
task = nullptr;
status = "Ожидает";
emit taskFinished(workerId);
}
}