Repository navigation
Expand file tree
/
Copy pathworker.cpp
More file actions
102 lines (79 loc) · 2.75 KB
/
Copy pathworker.cpp
File metadata and controls
102 lines (79 loc) · 2.75 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
/*
Copyright 2013 Fabien Pierre-Nicolas.
- Primarily authored by Fabien Pierre-Nicolas
This file is part of simple-qt-thread-example, a simple example to demonstrate how to use threads.
This example is explained on http://fabienpn.wordpress.com/qt-thread-simple-and-stable-with-sources/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This progra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "worker.h"
#include <QTimer>
#include <QEventLoop>
#include <QDebug>
Worker::Worker(QObject *parent) :
QObject(parent)
{
_working =false;
_abort = false;
}
void Worker::requestWork()
{
mutex.lock();
_working = true;
_abort = false;
qDebug()<<"Request worker start in Thread "<<thread()->currentThreadId();
mutex.unlock();
emit workRequested();
}
void Worker::abort()
{
mutex.lock();
if (_working) {
_abort = true;
qDebug()<<"Request worker aborting in Thread "<<thread()->currentThreadId();
}
mutex.unlock();
}
void Worker::setId(int id)
{
this->id=id;
}
void Worker::doWork()
{
qDebug()<<"Starting worker process in Thread "<<thread()->currentThreadId();
for (int i = 0; i < 100; i++) {
// Checks if the process should be aborted
mutex.lock();
bool abort = _abort;
mutex.unlock();
if (abort) {
qDebug()<<"Aborting worker process in Thread "<<thread()->currentThreadId();
break;
}
// This will stupidly wait 1 sec doing nothing...
QEventLoop loop;
QTimer::singleShot(100*(qrand()%((10))+1), &loop, SLOT(quit()));
loop.exec();
qDebug()<<id<<": is not busy now ,plea next client ...";
emit notbusy (id);
// qsrand(qrand());
// Mat img(Size(320,240), CV_8UC3,cv::Scalar(qrand()%((i + 1)), qrand()%((i + 1)), qrand()%((i + 1))));
// Once we're done waiting, value is updated
// emit valueChanged(QString::number(i));
}
// Set _working to false, meaning the process can't be aborted anymore.
mutex.lock();
_working = false;
mutex.unlock();
qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId();
//Once 60 sec passed, the finished signal is sent
emit finished();
}