forked from czyt1988/czyBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadObject.cpp
More file actions
105 lines (98 loc) · 2.64 KB
/
ThreadObject.cpp
File metadata and controls
105 lines (98 loc) · 2.64 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
#include "ThreadObject.h"
#include <QThread>
#include <QDebug>
#include <QMutexLocker>
#include <QElapsedTimer>
#include <limits>
ThreadObject::ThreadObject(QObject *parent):QObject(parent)
,m_runCount(10)
,m_runCount2(std::numeric_limits<int>::max())
,m_isStop(true)
{
}
ThreadObject::~ThreadObject()
{
qDebug() << "ThreadObject destroy";
emit message(QString("Destroy %1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
}
void ThreadObject::setRunCount(int count)
{
m_runCount = count;
emit message(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
}
void ThreadObject::runSomeBigWork1()
{
{
QMutexLocker locker(&m_stopMutex);
m_isStop = false;
}
int count = 0;
QString str = QString("%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId());
emit message(str);
int process = 0;
while(1)
{
{
QMutexLocker locker(&m_stopMutex);
if(m_isStop)
return;
}
if(m_runCount == count)
{
break;
}
sleep(1);
int pro = ((float)count / m_runCount) * 100;
if(pro != process)
{
process = pro;
emit progress(((float)count / m_runCount) * 100);
emit message(QString("Object::run times:%1,m_runCount:%2").arg(count).arg(m_runCount2));
}
++count;
}
}
void ThreadObject::runSomeBigWork2()
{
{
QMutexLocker locker(&m_stopMutex);
m_isStop = false;
}
int count = 0;
QString str = QString("%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId());
emit message(str);
int process = 0;
QElapsedTimer timer;
timer.start();
while(1)
{
{
QMutexLocker locker(&m_stopMutex);
if(m_isStop)
return;
}
if(m_runCount2 == count)
{
break;
}
int pro = ((float)count / m_runCount2) * 100;
if(pro != process)
{
process = pro;
emit progress(pro);
emit message(QString("%1,%2,%3,%4")
.arg(count)
.arg(m_runCount2)
.arg(pro)
.arg(timer.elapsed()));
timer.restart();
}
++count;
}
}
void ThreadObject::stop()
{
QMutexLocker locker(&m_stopMutex);
emit message(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
m_isStop = true;
}