c++ - 并行线程,QT

标签 c++ multithreading qt

我是 QT 的新手,对工作线程没有太多经验。 我读过很多关于线程的指南,发现至少有 3 种方法可以使用线程。我对其中之一很感兴趣,有我的例子:我已经创建了类,我正在尝试在线程中使用它的功能。

.h

#ifndef IDENTIFICATOR_H_
#define IDENTIFICATOR_H_
#include <QtCore>
#include <QCoreApplication>

class identificator:public QObject {
Q_OBJECT
public:
    identificator(int i);
    virtual ~identificator();
private:
int id;
private slots:
void printID();
public:
void setID(int i);
int getID();
signals:
void finished();
};

.cpp

#include "identificator.h"
#include <stdio.h>
#include <QDebug>
identificator::identificator(int i) {
id=i;
}

identificator::~identificator() {
    // TODO Auto-generated destructor stub
}
void identificator::setID(int i)
{
id=i;
}
int identificator::getID()
{
return id;
}
void identificator::printID()
{
    for(int i=0;i<10;i++)
    {
qDebug()<<" "<<this->getID()<<" "<<i<<" "<<this->thread();
    }emit finished();

}

和main.cpp

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    printf("Hello\n");
    identificator *first=new identificator(0);
    identificator *second=new identificator(0);
    QThread *thread1=new QThread;
    QThread *thread2=new QThread;
    first->setID(3);
    second->setID(4);
    first->moveToThread(thread1);
    second->moveToThread(thread2);
    QObject::connect(thread1, SIGNAL(started()), first, SLOT(printID()));
    QObject::connect(thread2, SIGNAL(started()), second, SLOT(printID()));
    QObject::connect(first, SIGNAL(finished()), thread1, SLOT(quit()));
    QObject::connect(second, SIGNAL(finished()), thread2, SLOT(quit()));
    thread1->start();
    thread2->start();
    return a.exec();
}

我想,在控制台输出中会有这样的东西: 3个 4个 3个 3个 3个 4个 4个 但我只有 4个 4个 4个 4个 4个 4

非常需要您的帮助,谢谢!



我有下一个输出:

Hello
4 0 QThread(0x8053268)
3 0 QThread(0x8053250)
3 1 QThread(0x8053250)
3 2 QThread(0x8053250)
3 3 QThread(0x8053250)
3 4 QThread(0x8053250)
3 5 QThread(0x8053250)
3 6 QThread(0x8053250)
3 7 QThread(0x8053250)
3 8 QThread(0x8053250)
3 9 QThread(0x8053250)
4 1 QThread(0x8053268)
4 2 QThread(0x8053268)
4 3 QThread(0x8053268)
4 4 QThread(0x8053268)
4 5 QThread(0x8053268)
4 6 QThread(0x8053268)
4 7 QThread(0x8053268)
4 8 QThread(0x8053268)
4 9 QThread(0x8053268)

最佳答案

你的输出是正确的,你的程序运行正常

  • 线程 1 启动并在打印任何内容之前被抢占
  • 线程2启动,输出4,被中断
  • thread1有处理器,输出3十次,直到emit完成
  • thread2 重新拥有处理器,可以输出 4 9 次,直到它 emit 完成

一些事情:

  • printID中的迭代次数太少,看不到真实的 两个线程之间的抢占(即使在这种情况下 t2 被中断了一次)
  • qDebug 不刷新输出。它只是在末尾放了一个换行符。这意味着您可能会错过控制台上的一些输出行。完全有可能只看到打印的 1 个值。
  • 您不能指望从该代码得到特定的输出。十个 3 和十个 4 的任何排列都可能发生。
  • 似乎 qdebug 有一些锁,否则你甚至可以看到损坏的输出,因为你没有刷新。

尝试相同的练习超过 10 次迭代,没有刷新,没有输出同步

std::cout <<" "<<this->getID()<<" "<<i<<" "<<this->thread() << "\n";

和刷新(不需要同步)

std::cout <<" "<<this->getID()<<" "<<i<<" "<<this->thread() << std::endl;

关于c++ - 并行线程,QT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634870/

相关文章:

c++ - 将 Cuda CU 文件分离到许多其他文件和 OPENGL 编译问题

c# - Azure 云服务

python - 当在多线程中使用暴力破解时,它比单线程慢

java - 跟踪不同线程上传的文件

c++ - QMessageBox会阻塞Qt中整个主线程的运行吗?

c++ - QAxObject 没有为 COM 对象创建槽信号

c++ - 重复相同代码的较大函数与较小函数

c++ - 在这种情况下我们需要禁用默认的复制构造函数和赋值运算符?

c++ - 异常处理和打开文件?

c++ - 水平布局尺寸