Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] C++ thread does not work in Qt?
Qt 6.11 is out! See what's new in the release blog

[Solved] C++ thread does not work in Qt?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MohammadReza
    wrote on last edited by
    #1

    Hi friends.

    I try to use c++ thread (in 'thread' library in windows) but have 2 problem.

    Here is my simple code

    mainwindow.h
    @
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void foo2();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    #include <thread>

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

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

    void foo()
    {
    for(int i=0; i<1000000;i++)
    qDebug() << i;
    }

    void MainWindow::foo2()
    {
    for(int i=0; i<1000000;i++)
    qDebug() << i;
    }

    void MainWindow::on_pushButton_clicked()
    {
    std::thread t1(foo);
    }
    @

    'foo' doesn`t have a signature but 'foo2' has a signature.

    when I use 'foo' function in on_pushButton_clicked() i get this run time error:
    !http://8pic.ir/images/tbwhxd5kvdi8g3jaa7b1.jpg(http://8pic.ir/images/tbwhxd5kvdi8g3jaa7b1.jpg)!

    when I use 'foo2' function in on_pushButton_clicked() i get this error:
    error: C3867: 'MainWindow::foo2': function call missing argument list; use '&MainWindow::foo2' to create a pointer to member

    what is the problem?

    Thanks a lot.
    Ya Ali.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @
      std::thread t1(&MainWindow::foo2);
      @

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MohammadReza
        wrote on last edited by
        #3

        Thanks dear sierdzio but another errors occured:

        !http://8pic.ir/images/qdlr3b7qh19it9vec7f9.jpg(http://8pic.ir/images/qdlr3b7qh19it9vec7f9.jpg)!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MohammadReza
          wrote on last edited by
          #4

          Thanks be to GOD. I could solve this problem by defining foo2 as a static function but run time error occoured again. That is, now foo & foo2 has the runtime error!

          How can I solve the runtime error?

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Please run it with the debugger attached to see where it fails.

            (Z(:^

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MohammadReza
              wrote on last edited by
              #6

              There is no problem in code.
              I get the runtime error here:

              !http://8pic.ir/images/6xtyikmiv4go892yshbk.jpg(http://8pic.ir/images/6xtyikmiv4go892yshbk.jpg)!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MohammadReza
                wrote on last edited by
                #7

                Thanks be to GOD. I must detach or join the thread like this:

                @
                void MainWindow::on_pushButton_clicked()
                {
                std::thread t1(foo);
                t1.join();
                }
                @

                Reference: http://stackoverflow.com/questions/15423345/c-stdthread-problems-in-visual-studio-2012

                Thanks to dear sierdzio for helping me.

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  That is NT dll, not interesting in this case. Go back in the stack trace to see where it fails in your code. I would suspect you are using STD threads wrongly, but I am not sure, I always use QThread instead.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Another point, why use a thread since the first thing you do after creating t1 is to join so you block the calling thread until t1 has finished ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved