-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paththread.cc
81 lines (68 loc) · 1.5 KB
/
thread.cc
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
#include "thread.hpp"
#include "functions.hpp"
#include "task.hpp"
#include <QDebug>
#include <QThreadPool>
#include <QtConcurrent>
void Thread1::run()
{
qDebug() << "Thread1----" << Functions::getCurrentThreadIDStr();
}
Thread2::~Thread2()
{
if (isRunning()) {
quit();
wait();
}
}
void Thread2::onDo()
{
emit doo("Thread2----");
}
void Thread2::run() // 类似 moveToThread
{
QScopedPointer<Task> taskPtr(new Task);
connect(this, &Thread2::doo, taskPtr.data(), &Task::onDo);
exec();
}
Thread3::Thread3(QObject *parent)
: QObject(parent)
, m_task(new Task)
, m_thread(new QThread)
{
m_task->moveToThread(m_thread);
connect(m_thread, &QThread::finished, m_task, &Task::deleteLater);
connect(this, &Thread3::doo, m_task, &Task::onDo);
m_thread->start();
}
Thread3::~Thread3()
{
if (m_thread->isRunning()) {
m_thread->quit();
m_thread->wait();
}
}
void Thread3::dooo()
{
emit doo("Thread3----");
}
class Runnable : public QRunnable
{
public:
using QRunnable::QRunnable;
protected:
void run() override { qDebug() << "Thread4----" << Functions::getCurrentThreadIDStr(); }
};
void runThread4()
{
QThreadPool::globalInstance()->start(new Runnable);
}
void runThread5()
{
auto ret = QtConcurrent::run(
[] { qDebug() << "Thread5----" << Functions::getCurrentThreadIDStr(); });
}
QThread *runThread6()
{
return QThread::create([] { qDebug() << "Thread6----" << Functions::getCurrentThreadIDStr(); });
}