-1

I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following 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;
}

On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C Please, I need your help!

1 Answers1

3

You try to use QThread subclass, but you said that you want to use C++11 thread so use this:

#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>

void foo()
{
    std::cout << "This string from thread!"<<endl;
    //while(true)
    //{
    //    qDebug() <<"works";
    //    Sleep(500);
    //}
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    std::thread t(foo);
    t.join();
    return a.exec();
}

Also next is wrong:

   MyThread th1;
   th1.run();

Should be:

   MyThread th1;
   th1.start();

Details about threading with Qt classes:

https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong

http://qt-project.org/doc/qt-5/thread-basics.html

http://qt-project.org/doc/qt-5/qtconcurrent-index.html

ololuki
  • 377
  • 1
  • 7
  • 14
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • @CSGOpetruska I don't know what you use so check solutions from this two questions: http://stackoverflow.com/questions/8513980/how-to-compile-the-code-using-include-thread http://stackoverflow.com/questions/18367147/cannot-open-include-file-thread – Jablonski Nov 02 '14 at 19:22