QT多线程选项
来源:互联网 发布:飞天侠淘宝干嘛的 编辑:程序博客网 时间:2024/05/20 05:45
QT多线程选项:
1、QThread subclassing
MyThread::MyThread()
{
// In creating thread
}
MyThread::~MyThread()
{
// In creating thread
}
MyThread::run()
{
// In creating thread
// Don't use this as parent
// Make sure all objects in thread are
// deleted before the thread is done
}
2、QThread worker threads
class MyObject : public QObject
{
Q_OBJECT
public slots:
void doStuff() { /* process here */ emit done(); }
signals:
void done();
}
QThread workerThread;
MyObject object;
object.movetoThread(&workerThread);
connect(workerThread, started, object, doStuff);
connect(object, done, workerThread, quit);
workerThread.start();
3、QRunnable and thread pools
class Worker : public QRunnable
{
void run() { /* process workload here */ }
}
Worker* worker = new Worker(itemToWorkOn);
QThreadPool::globalInstance()->start(worker);
4、QtConcurrent::run
QList<int> complicatedDBSearch();
QFutureWatcher<QList<int> > watcher;
connect(watcher, finished, someObject, dbListReady);
QFuture<QList<int> > idList = QtConcurrent::run(complicatedDBSearch);
watcher.setFuture(idList);
5、QtConcurrent maps and filters
qreal studentAverage(int studentID);
QFuture<qreal> future = QtConcurrent::mapped(allStudents, studentAverage);
qDebug() << future.results();
void add(qreal& g1, qreal g2) { g1 += g2; }
QFuture<qreal> averages = QtConcurrent::mappedReduced(allStudents, studentAverage, add);
schoolAverage = averages.results()[0] / studentCount;
void idToHtml(QString s&, int id) { ... }
bool studentsNamedBo(int id) {
return getStudentName(id).startsWith("Bo");
}
QFuture<qreal> webPage = QtConcurrent::filteredReduced(allStudents, studentBo, idToHtml);
html = htmlTop + webPage.results()[0] + htmlBottom;
Recommendations:
QThread subclassing: pure processing
QThread worker: "Normal" MT Qt code
QRunable and QThreadPool: For separate worker chunks
Map-reduce and filter-reduce: When the problems allows it
QtConcurrent: Whenever possible
1、QThread subclassing
MyThread::MyThread()
{
// In creating thread
}
MyThread::~MyThread()
{
// In creating thread
}
MyThread::run()
{
// In creating thread
// Don't use this as parent
// Make sure all objects in thread are
// deleted before the thread is done
}
2、QThread worker threads
class MyObject : public QObject
{
Q_OBJECT
public slots:
void doStuff() { /* process here */ emit done(); }
signals:
void done();
}
QThread workerThread;
MyObject object;
object.movetoThread(&workerThread);
connect(workerThread, started, object, doStuff);
connect(object, done, workerThread, quit);
workerThread.start();
3、QRunnable and thread pools
class Worker : public QRunnable
{
void run() { /* process workload here */ }
}
Worker* worker = new Worker(itemToWorkOn);
QThreadPool::globalInstance()->start(worker);
4、QtConcurrent::run
QList<int> complicatedDBSearch();
QFutureWatcher<QList<int> > watcher;
connect(watcher, finished, someObject, dbListReady);
QFuture<QList<int> > idList = QtConcurrent::run(complicatedDBSearch);
watcher.setFuture(idList);
5、QtConcurrent maps and filters
qreal studentAverage(int studentID);
QFuture<qreal> future = QtConcurrent::mapped(allStudents, studentAverage);
qDebug() << future.results();
void add(qreal& g1, qreal g2) { g1 += g2; }
QFuture<qreal> averages = QtConcurrent::mappedReduced(allStudents, studentAverage, add);
schoolAverage = averages.results()[0] / studentCount;
void idToHtml(QString s&, int id) { ... }
bool studentsNamedBo(int id) {
return getStudentName(id).startsWith("Bo");
}
QFuture<qreal> webPage = QtConcurrent::filteredReduced(allStudents, studentBo, idToHtml);
html = htmlTop + webPage.results()[0] + htmlBottom;
Recommendations:
QThread subclassing: pure processing
QThread worker: "Normal" MT Qt code
QRunable and QThreadPool: For separate worker chunks
Map-reduce and filter-reduce: When the problems allows it
QtConcurrent: Whenever possible
0 0
- QT多线程选项
- QT 环境配置选项
- Qt-configure配置选项
- Qt configure选项
- Qt-configure配置选项
- Qt多线程
- Qt多线程
- Qt多线程
- qt 多线程
- Qt多线程
- QT多线程
- QT多线程
- QT多线程
- Qt多线程
- qt 多线程
- Qt多线程
- QT 多线程
- qt 多线程
- 设计模式学习之模版方法模式
- 虚函数浅析
- 引用类型的赋值问题--.net 中的Deep Clone
- 数据结构与算法(c++)--拓扑排序
- DAY_01 HTML小白学习笔记
- QT多线程选项
- webpack
- 【应用实例】增强现实应用开发—3D模型、视频、音频、交互
- 在QT集成开发环境中用OpenCV读取一张图片显示在Qlabel上
- 【Unity3D_UGUI速成班】——07.Scrollbar
- 从函数的零点问题到二分法的应用
- c语言之堆区二维数组
- ECSHOP自动确认收货解决方案 【附代码】
- Implement the merge sort