std::thread 使用 Qt

std::thread with Qt

作为学习示例,我正在尝试使用 Qt 而不是 QThreads 来测试 std::thread。该应用程序是一个非常基本的 QMainWindow 应用程序,请参见下面的代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    [[ noreturn ]] void operator()();

private:
    Ui::MainWindow *ui;
    QString mm;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

[[ noreturn ]] void MainWindow::operator()()
{
    qDebug()<< "thread runing";
    int i =0;
    while (1)
    {
        i++;
    }
}

main.cpp

#include <thread>
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWindow;
    mainWindow.show();
    std::thread t(&mainWindow);
    t.detach();
    return app.exec();
}

此代码无法编译并产生错误:

In file included from /home/faroub/Documents/development-projects/projects-c++/Qt-CMake-GUI/HelloWorld/main.cpp:1:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<MainWindow*> >’:
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = MainWindow*; _Args = {}]’
/home/faroub/Documents/development-projects/projects-c++/Qt-CMake-GUI/HelloWorld/main.cpp:10:30:   required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<MainWindow*> >::_M_invoke(std::thread::_Invoker<std::tuple<MainWindow*> >::_Indices)’
  operator()()
  ^~~~~~~~

知道为什么它不起作用吗?我必须使用 Qt 来使用 QThreads 吗?它与QObject有关吗?提前谢谢你。

与其将指针传递给线程构造函数,不如将其传递给 std::reference_wrapper,如下所示:

std::thread t(std::ref(mainWindow));

该包装器来自 <functional> header。

您尝试传递引用(按地址)是正确的,因为如果不这样做,就会创建 MainWindow 的副本(不是您想要的)。但是 std::thread 中没有有效的构造函数可以接受一个指向仿函数的指针并调用它。