首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在第三方函数并发调用前运行线程?

如何在第三方函数并发调用前运行线程?
EN

Stack Overflow用户
提问于 2018-01-25 00:28:22
回答 1查看 80关注 0票数 0

我创建了一个工作线程任务,另一个任务是我的公共第三方函数。我想同时运行线程槽(e.g:StartWork())和我的公共函数(e.g:on_pushButton_4_clicked()),但它们不会,请告诉我怎么做。下面是我的代码:

Mythread.cpp

代码语言:javascript
运行
复制
#include "mythread.h"
#include "ui_mythread.h"
Mythread::Mythread(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Mythread)
{
    ui->setupUi(this);
    //Debug
    this->dumpObjectInfo();
    //myWorker->dumpObjectInfo();
}

Mythread::~Mythread()
{
    myWorker->abort();
    WorkerThread->wait();
    qDebug()<<"Deleting thread and worker in Thread "<<this->QObject::thread()->currentThreadId();
    delete WorkerThread;
    delete myWorker;
    delete ui;
}
void Mythread::on_pushButton_2_clicked()
{
    qDebug() << "stopwork signal emmitted";
    emit stopWorkSignal();
}

void Mythread::on_pushButton_4_clicked()
{
    myWorker = new worker;
    WorkerThread = new QThread;
    myWorker->moveToThread(WorkerThread);
    WorkerThread->start();

    connect(myWorker, SIGNAL(valueChanged(QString)),this, SLOT(myfunc(QString)));
    connect(WorkerThread, SIGNAL(started()), myWorker, SLOT(StartWork()));
    connect(this, SIGNAL(stopWorkSignal()), myWorker, SLOT(abort()));
    qDebug()<<"inside work";
    int i =0;
    while (i<1000)
    {
    qDebug()<<":count *i=========>"<<i;
    i++;
    }
    return;
}

void Mythread::myfunc(QString s)
{
    qDebug()<<"thread TXT"<<s;
    //
    ui->label->setText(s);
}

worker.cpp

代码语言:javascript
运行
复制
#include "worker.h"
#include<QDebug>
#include<mythread.h>

worker::worker(QObject *parent) :
    QObject(parent)
{
    _working =false;
    _abort = false;
}

void worker::do_Work()
{
    qDebug() << "inside do Work";
    qDebug()<<"Starting worker process in Thread   "<<thread()->currentThreadId();
    for (int i = 1; i<100; i ++) {
    // Checks if the process should be aborted
    mutex.lock();
    bool abort = _abort;
    mutex.unlock();
    if (abort) {
        qDebug()<<"Aborting worker process in Thread "<<thread()->currentThreadId();
        break;
    }

    // This will stupidly wait 1 sec doing nothing...
    QEventLoop loop;
    loop.processEvents(QEventLoop::AllEvents);
    QTimer::singleShot(100, &loop, SLOT(quit()));
    loop.exec();

     //Once we're done waiting, value is updated
    emit valueChanged(QString("%1").arg(i++));
    }

    mutex.lock();
    _working = false;
    mutex.unlock();

    qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId();
    emit finished();
}


void worker::abort()
{
    qDebug()<<"Stop Thread";
    mutex.lock();
    if (_working) {
    _abort = true;
    qDebug()<<"Request worker aborting in Thread "<<thread()->currentThreadId();
    }
    mutex.unlock();
    emit finished();
    //lbl->close();
    //lbl->deleteLater();
}
void worker::StartWork()
{
    qDebug() << "inside StartWork";
    _working = true;
    _abort = false;

    //emit running();
    do_Work();
}

输出结果如下:

代码语言:javascript
运行
复制
  :count *i=========> 988 
  :count *i=========> 989 
  :count *i=========> 990 
  :count *i=========> 991 
  :count *i=========> 992 

 thread TXT "1" 
 thread TXT "3" 
 thread TXT "5" 
 thread TXT "7" 
EN

回答 1

Stack Overflow用户

发布于 2018-01-28 04:03:25

以下是几个问题:

  • 您在启动线程后连接QThread::started to StartWork() ,所以很可能您错过了信号。
  • 执行一个1000的while()循环是在很短的时间内完成的,所以它很可能会在您有机会向另一个线程发出信号之前运行。
  • 最重要的问题是,当您在线程上发出valueChanged()时,它将在myFunc()上排队到您的主线程。

你可以随时调用QThread::currentThread()来查看它是否在正确的线程上运行,这也取决于操作系统来调度你的线程,所以期望看到两个线程的输出不太可能像你试图做的那样同时发生。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48427265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档