c++ - 如何使用多线程制作一个简单的 Qt 控制台应用程序?

标签 c++ multithreading qt console

我很难理解如何尽可能简单地工作 多线程 Qt 控制台 应用程序。

我已经阅读了大量有关如何使用 QThread 类的内容。 其中一些人说子类 QThread,另一些人说使用 QThread 的工作类包装器。

经过几次尝试和重试,我仍然无法使多线程工作 Qt 控制台应用程序。

现在我不需要任何花哨的 Qt Gui。

有人可以帮我填充示例代码的线程部分吗? 它每次只从文本文件中读取一行,其想法是,当前不忙的每个线程(我想使用 4 个线程)将尽快使用 std::cout 将该行打印到 stdout。只需打印它,暂时没有其他花哨的处理内容,以保持这对我来说简单。

#include <QCoreApplication>
#include <QFile>
#include <iostream>

/* QThread stuff here */
/* Don't know how to make it */

int main(int argc, char *argv[])
{
        QCoreApplication a(argc, argv);

        /* Create four instances of threads here and 
           put them to wait readed lines */

    QFile   file("file.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    while(!file.atEnd()) {
    /* Read lines here but where should they be saved?
       Into a global variable like QList<QByteArray> list ?
       So that each thread can read them from there or where ???? */

       ??? = file.readLine(); 
    }
    file.close();
    a.exit();
}

最佳答案

将功能放入 QObject 的槽中

要点是:

  1. 请记住,每个QObject都有一个它“居住”的特定thread()。每个线程都可以有一个在那里运行的事件循环。此事件循环将发送发送到该线程中“存在”的对象的事件。

  2. 不要从QThread派生。启动库存QThreads。他们将在 QThread::run() 的默认实现中启动偶数事件循环。

  3. 在槽(或 Q_INVOKABLE)方法中实现您的功能。该类显然必须从 QObject 派生。

  4. 当您将信号(使用信号槽连接,而不是直接)发送到 #3 中的槽时,奇迹就会发生。从 GUI 线程中运行的通知程序到被通知对象的连接是使用 Qt::QueuedConnection 自动完成的,因为发送方和接收方对象位于不同的线程中。

    向此类对象发送信号会导致将事件发布到该对象所在线程的事件队列。事件循环的事件调度程序将选择这些事件并调用适当的槽。这就是 Qt 的力量 - 可以为您完成很多有用的事情。

请注意,没有“当前繁忙”线程的概念。线程执行存在于其中的对象的短槽。如果您想在“忙”和“不忙”状态之间移动线程,那么您需要额外的代码。

实现它的另一种方法是从QRunnable派生并使用QThreadPool。这是另一个答案。

ma​​in.cpp

#include <QCoreApplication>
#include <QTextStream>
#include <QThread>
#include <QFile>
#include <cstdio>

class Notified : public QObject {
    Q_OBJECT
    QTextStream m_out;
public:
    Q_SLOT void notify(const QString & text) {
        m_out << "(" << this << ") " << text << endl;
    }
    Notified(QObject *parent = 0) : QObject(parent), m_out(stdout) {}
};

class Notifier : public QObject {
    Q_OBJECT
    Q_SIGNAL void notification(const QString &);
public:
    Notifier(QObject *parent = 0) : QObject(parent) {}
    void notifyLines(const QString & filePath) {
        QFile file(filePath);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        while (! file.atEnd()) {
            emit notification(file.readLine());
        }
        file.close();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QObjectList notifieds;
    QList<QThread*> threads;
    Notifier notifier;

    for (int i = 0; i < 4; ++i) {
        QThread * thread = new QThread(&a); // thread owned by the application object
        Notified * notified = new Notified; // can't have an owner before it's moved to another thread
        notified->moveToThread(thread);
        thread->start();
        notifieds << notified;
        threads << thread;
        notified->connect(&notifier, SIGNAL(notification(QString)), SLOT(notify(QString)));
    }

    notifier.notifyLines("file.txt");

    foreach (QThread *thread, threads) {
        thread->quit();
        thread->wait();
    }
    foreach (QObject *notified, notifieds) delete notified;

    a.exit();
}

#include "main.moc"

关于c++ - 如何使用多线程制作一个简单的 Qt 控制台应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18656361/

相关文章:

c++ - 如何处理这种情况(需要注册可变参数的函数)

c++ - 我怎样才能实现一个通用的最小值?

c++ - C++中的元最小值

c++ - 170 MB Hello World -> 使用 Qt 部署应用程序

Java:多线程和 UDP 套接字编程

Java 线程可运行/可调用

qt - 使 QLabel 的像素图透明

java - 使用异步 http 提高吞吐量

c++ - Qt Creator - 无法使用调试器 : program. exe 不是可执行格式:无法识别文件格式

qt - 更改 QTableView 的默认选择颜色