Good day.

Began to learn threads for 11 standard in Qt!

Here I have a simple code:

#include <QCoreApplication> #include <thread> #include <iostream> using namespace std; void func(); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyThread th1; th1.run(); return a.exec(); } void func() { cout << "This string from thread!"<<endl; } 

So, on the second line (the thread library) the compiler swears, they say, does not know this.
I read that you need in the file with the .pro extension to add CONFIG += c++11 . Finished, but nothing has changed!

Tell me please.

Update

 #include <QDebug> #include <QCoreApplication> #include <iostream> #include <QThread> void foo() { std::cout << "This string from thread!"<<endl; } int main(int argc, char *argv[]) { QApplication a(argc, argv); std::thread t(foo); std::t.join(); return a.exec(); } 

Why does the compiler swear?

  • Please try to write more informative question headings so that you can understand the context in any way. - Violet Giraffe
  • What compiler? What OS? qmake re-called after editing a .pro file? - Violet Giraffe

2 answers 2

And where is your MyThread class declared? In Qt, there is no such thing; in the standard library, there is no such thing either, it is called std :: thread there .

upd

If you want to use threads from c ++ 11, here is the working code

 #include <iostream> #include <thread> void foo() { std::cout << "This string from thread!"<<std::endl; } int main(int argc, char *argv[]) { std::thread t(foo); t.join(); return 0; } 

compile like that

 g++ thr.cpp -std=c++11 -pthread 

And it works perpendicular to Qt.

  • Updated the question - petruska
  • Because now you have connected the Qt thread library, and in the code you are trying to use it from the standard library. - KoVadim

And why not implement the flow as specified in the Qt documentation. There is a great QThread class. The only thing you need to learn to work with him. Here is an excellent article on the topic of multithreading in Qt http://habrahabr.ru/post/202312/