faroub
faroub

Reputation: 23

std::thread with Qt

As a learning example I am trying to test std::thread with Qt instead of QThreads. The application is a very basic QMainWindow application,see code bellow:

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();
}

This code does not compile and produces the error:

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()()
  ^~~~~~~~

Any ideas why it does not work ? Do I have to QThreads with Qt? is it related to the QObject? thank you in advance.

Upvotes: 1

Views: 1795

Answers (1)

rems4e
rems4e

Reputation: 3172

Instead of passing a pointer to your thread constructor, pass it a std::reference_wrapper like this:

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

That wrapper comes from the <functional> header.

You were right to try to pass a reference (by address), because if not then a copy of the MainWindow would have been created (not what you want). But there is no valid constructor in std::thread that would take a pointer to a functor and call it.

Upvotes: 1

Related Questions