我有一个主线程,它在不同的时间调用子线程函数,但我不确定这是否是在Qt中这样做的正确方法。以下代码出错并寻找更好的替代方案
当主线程释放锁定子进行一项工作时,有一个无限运行的主线程。
#include <QtCore/QCoreApplication>
#include <QSemaphore>
#include <QThread>
QSemaphore sem(0);
class Background : public QThread
{
protected:
void run()
{
for(;;)
{
sem.acquire(1);
qDebug("Child function ran");
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Background child;
child.start();
qDebug("Main running");
qDebug("release a lock");
sem.release(1);
qDebug("Do somework in main");
//call child
sem.release(1);
sem.release(1);
return a.exec();
}
答案 0 :(得分:4)
修改:修改整个帖子以涵盖基础知识。
Background.h
:
#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <QThread>
#include <QObject>
class Background : public QThread
{
Q_OBJECT
public:
Background(QObject* parent = 0):QThread(parent){}
protected:
void run()
{
qDebug(qPrintable(QString("Child function ran in thread: %1").arg(QThread::currentThreadId())));
}
};
class BackgroundConcurrent : public QObject
{
Q_OBJECT
public:
BackgroundConcurrent(QObject* parent = 0):QObject(parent){}
public slots:
void doWork() const
{
qDebug(qPrintable(QString("Concurrent child function ran in thread: %1").arg(QThread::currentThreadId())));
}
};
class BackgroundTrigger : public QObject
{
Q_OBJECT
public:
BackgroundTrigger(QObject* parent = 0):QObject(parent){}
~BackgroundTrigger()
{
foreach(QObject* child, children())
{
QThread* childThread = qobject_cast<QThread*>(child);
if (childThread)
childThread->wait();
}
}
public slots:
void triggerWorker()
{
Background* child = new Background(this);
child->start();
}
};
#endif // BACKGROUND_H
main.cpp
:
#include "Background.h"
#include <QCoreApplication>
#include <QtConcurrentRun>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Using QThread
BackgroundTrigger childTrigger;
qDebug(qPrintable(QString("Main function ran in thread: %1").arg(QThread::currentThreadId())));
// Call child
childTrigger.triggerWorker();
childTrigger.triggerWorker();
// Using QtConcurrent
BackgroundConcurrent cchild;
QFuture<void> future1 = QtConcurrent::run(&cchild, &BackgroundConcurrent::doWork);
QFuture<void> future2 = QtConcurrent::run(&cchild, &BackgroundConcurrent::doWork);
return 0;
}
示例输出:
Main function ran in thread: 1087038064 Child function ran in thread: 1091267472 Child function ran in thread: 1093417872 Concurrent child function ran in thread: 1095519120 Concurrent child function ran in thread: 1097644944
请确保在头文件上运行moc,qmake和cmake都支持创建makefile。
以下是我用来构建代码的CMakeLists.txt
文件:
cmake_minimum_required(VERSION 2.6)
#Project name
project(TEST)
#Use Qt4
find_package(Qt4)
if(QT4_FOUND)
set(QT_USE_QTOPENGL TRUE)
include(${QT_USE_FILE})
set(LIBS
${QT_LIBRARIES}
)
#Source files (*.cpp, *.o)
set(TEST_SRCS main.cpp)
#Header files (*.h[pp])
set(TEST_HDRS Background.h)
#Qt macros to handle uic, moc, etc...
QT4_WRAP_CPP(TEST_MOC ${TEST_HDRS} OPTIONS -nw)
set(TEST_ALLSRC ${TEST_SRCS} ${TEST_MOC})
#Create main
add_executable(test ${TEST_ALLSRC})
target_link_libraries(test ${LIBS})
endif(QT4_FOUND)
答案 1 :(得分:3)
实际上你目前解决这个问题的方法非常好。
如果您希望以“更干净”的方式进行,则应在工作线程中启动事件循环。然后工作线程将能够从主线程接收信号。您可以从主线程调用子线程中的函数(使用信号/插槽机制)来触发操作。
请点击此处了解更多详情: http://doc.trolltech.com/4.2/threads.html#per-thread-event-loop
(提示:关键的想法是你在工作线程中创建接收对象;然后它的插槽将在该线程中处理;或者你可以使用MoveToThread()函数)
答案 2 :(得分:0)
似乎你可能想要使用一个信号,以适应正常的Qt风格。查看this question;接受的答案似乎也与你的问题相符。