[Solved] C++ thread does not work in Qt?
-
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_OBJECTpublic:
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 memberwhat is the problem?
Thanks a lot.
Ya Ali. -
@
std::thread t1(&MainWindow::foo2);
@ -
Thanks dear sierdzio but another errors occured:
!http://8pic.ir/images/qdlr3b7qh19it9vec7f9.jpg(http://8pic.ir/images/qdlr3b7qh19it9vec7f9.jpg)!
-
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?
-
Please run it with the debugger attached to see where it fails.
-
There is no problem in code.
I get the runtime error here:!http://8pic.ir/images/6xtyikmiv4go892yshbk.jpg(http://8pic.ir/images/6xtyikmiv4go892yshbk.jpg)!
-
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.
-
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.
-
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 ?