C++ 带有 movetothread 的 qt 线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11033971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:46:14  来源:igfitidea点击:

qt thread with movetothread

c++multithreadingqt

提问by unfamous

I'm trying to create a program using threads: the main start with a loop. When a test returns true, I create an object and I want that object to work in an other thread then return and start the test .

我正在尝试使用线程创建一个程序:主要以循环开始。当测试返回 true 时,我创建一个对象,我希望该对象在另一个线程中工作,然后返回并开始测试。

QCoreApplication a(argc, argv);
while(true){
    Cmd cmd;
    cmd =db->select(cmd);
    if(cmd.isNull()){ 
        sleep(2);  
        continue ;
    }
    QThread *thread = new QThread( );
    process *class= new process ();
    class->moveToThread(thread);
    thread->start();

    qDebug() << " msg"; // this doesn't run until  class finish it's work 
}
return a.exec();

the problem is when i start the new thread the main thread stops and wait for the new thread's finish .

问题是当我启动新线程时,主线程停止并等待新线程完成。

回答by smerlin

The canonical Qt way would look like this:

规范的 Qt 方式如下所示:

 QThread* thread = new QThread( );
 Task* task = new Task();

 // move the task object to the thread BEFORE connecting any signal/slots
 task->moveToThread(thread);

 connect(thread, SIGNAL(started()), task, SLOT(doWork()));
 connect(task, SIGNAL(workFinished()), thread, SLOT(quit()));

 // automatically delete thread and task object when work is done:
 connect(task, SIGNAL(workFinished()), task, SLOT(deleteLater()));
 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

 thread->start();

in case you arent familiar with signals/slots, the Task class would look something like this:

如果您不熟悉信号/插槽,Task 类将如下所示:

class Task : public QObject
{
Q_OBJECT
public:
    Task();
    ~Task();
public slots:
    // doWork must emit workFinished when it is done.
    void doWork();
signals:
    void workFinished();
};

回答by jlunavtgrad

I don't know how you structured your process class, but this is not really the way that moveToThread works. The moveToThread function tells QT that any slots need to be executed in the new thread rather than in the thread they were signaled from. (edit: Actually, I now remember it defaults to the tread the object was created in)

我不知道您是如何构建流程类的,但这并不是 moveToThread 真正的工作方式。moveToThread 函数告诉 QT 任何插槽都需要在新线程中执行,而不是在它们发出信号的线程中执行。(编辑:实际上,我现在记得它默认为创建对象的胎面)

Also, if you do the work in your process class from the constructor it will not run in the new thread either.

此外,如果您从构造函数在您的进程类中完成工作,它也不会在新线程中运行。

The simplest way to have your process class execute in a new thread is to derive it from QThread and override the run method. Then you never need to call move to thread at all.

让进程类在新线程中执行的最简单方法是从 QThread 派生它并覆盖 run 方法。那么你根本不需要调用 move to thread 。