Results 1 to 8 of 8

Thread: How can i use thread from C++11 in qt creator

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    33
    Thanked 2 Times in 2 Posts

    Default How can i use thread from C++11 in qt creator

    Hello All.c
    I have the latest mingw version (4.7.1) and i have it set-up and successfully ran a sample file (which i post below)in CodeBlocks using the mention compiler.
    Now i want to use std::thread in Qt. when i try to compile this sample:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include <vector>
    4. #include <thread>
    5.  
    6.  
    7. void func(int tid)
    8. {
    9. std::cout << "Launched by thread " << tid << std::endl;
    10. }
    11.  
    12.  
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QCoreApplication a(argc, argv);
    17.  
    18. std::vector<std::thread> th;
    19.  
    20. int nr_threads = 10;
    21.  
    22. for (int i = 0; i < nr_threads; ++i)
    23. {
    24. th.push_back(std::thread(func,i));
    25. }
    26.  
    27. for(auto &item : th)
    28. {
    29. item.join();
    30. }
    31.  
    32.  
    33. return a.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    I get an error saying :
    C:\Users\Master\Documents\Qt\ThreadCpp11\main.cpp: 4: error: C1083: Cannot open include file: 'thread': No such file or directory
    .
    What other steps are necessary in order to get this to work in QtCreator?(QT4.8.2)
    here are some pics showing my settings :



    Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How can i use thread from C++11 in qt creator

    This is between you and your C++ compiler: not Qt.

    The error message you have given is from a Microsoft compiler, not MingW. AFAICT (here for example) there is no C++11 thread support in MSVC10. Consequently, there's no <thread> header to include with the Microsoft compiler you are using.

    If you do use MingW/g++ 4.7 then you will need to add at least "-std=c++11" to your compiler options (using QMAKE_CXXFLAGS) in order to invoke the experimental C++11 support.

  3. The following user says thank you to ChrisW67 for this useful post:

    Hossein (4th September 2012)

  4. #3
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    33
    Thanked 2 Times in 2 Posts

    Default Re: How can i use thread from C++11 in qt creator

    Quote Originally Posted by ChrisW67 View Post
    This is between you and your C++ compiler: not Qt.

    The error message you have given is from a Microsoft compiler, not MingW. AFAICT (here for example) there is no C++11 thread support in MSVC10. Consequently, there's no <thread> header to include with the Microsoft compiler you are using.

    If you do use MingW/g++ 4.7 then you will need to add at least "-std=c++11" to your compiler options (using QMAKE_CXXFLAGS) in order to invoke the experimental C++11 support.
    Thanks.But i believe that i set the QtCreators compiler to Mingwgcc 4.7.1 and they are evident in the pictures i posted.I know that VS2010 doesnt support C++11,butMingw(gcc port on windows ) does.Would you please be more specific in terms of how i can configure QtCreator for such a task.I believe i did the conventional configuration though

    ----------
    Edited:
    I noticed the VC11 compiler is still active , How can i set Mingw as the default compiler?apparently choosing Mingw from Auto-Detected list from ToolChain tab in QtCreator is not the case because it just doesnt work!:-/
    Last edited by Hossein; 4th September 2012 at 06:08.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How can i use thread from C++11 in qt creator

    • Select tool chain
    • Click Apply or OK
    • Do a complete rebuild of the project including running qmake. If you do not rerun qmake then the old Makefile, full of MSVC commands, will still be present.


    You should do something like this in the PRO file:
    Qt Code:
    1. win32-g++ {
    2. QMAKE_CXXFLAGS += -std=c++11
    3. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    Hossein (5th September 2012)

  7. #5
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    33
    Thanked 2 Times in 2 Posts

    Default Re: How can i use thread from C++11 in qt creator

    Quote Originally Posted by ChrisW67 View Post
    • Select tool chain
    • Click Apply or OK
    • Do a complete rebuild of the project including running qmake. If you do not rerun qmake then the old Makefile, full of MSVC commands, will still be present.


    You should do something like this in the PRO file:
    Qt Code:
    1. win32-g++ {
    2. QMAKE_CXXFLAGS += -std=c++11
    3. }
    To copy to clipboard, switch view to plain text mode 
    still not a go
    Here are the screenshots showing what i have done. :



    what am i missing?

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How can i use thread from C++11 in qt creator

    The Tools Options panel is for configuring what tool chains are available to Qt Creator, and which one to use if you open a previously unused project.

    The Projects, Build Settings panel is where you set the tool chain being used by a given project.
    e2f0Untitled2.jpg
    Your project is using the Microsoft tool chain.

  9. The following user says thank you to ChrisW67 for this useful post:

    Hossein (6th September 2012)

  10. #7
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    33
    Thanked 2 Times in 2 Posts

    Default Re: How can i use thread from C++11 in qt creator

    Quote Originally Posted by ChrisW67 View Post
    The Tools Options panel is for configuring what tool chains are available to Qt Creator, and which one to use if you open a previously unused project.

    The Projects, Build Settings panel is where you set the tool chain being used by a given project.
    e2f0Untitled2.jpg
    Your project is using the Microsoft tool chain.
    Thank you I tried to add Mingw tool chain through the same pane you just showed to me(using Manage button) , when i click apply and then ok to either select MingW(x86) from the tool chains list or a manual Mingw tool-chain, nothing is added to the tool-chain ComboBox inside projects/build settings!so that i can change the tool-chain to mingw from visual C++ compiler
    It is driving me absolutely crazy.

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How can i use thread from C++11 in qt creator

    It is in the Qt Creator manual under Managing Projects, Configuring Projects, Specifying Build Settings. A project can have many build configurations. Yours current has two, one debug and one release, bot MSVC. You want to add another two for MingW and possibly remove the MSVC ones. Click the "Add" button at the top of the Build Settings panel to add more allowable build configurations for this project. Then select the one you want to use when you build.

  12. The following user says thank you to ChrisW67 for this useful post:

    Hossein (7th September 2012)

Similar Threads

  1. Replies: 1
    Last Post: 4th September 2012, 14:13
  2. Replies: 1
    Last Post: 28th March 2012, 18:58
  3. thread name in Qt Creator thread window
    By GoAway in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2011, 19:01
  4. Compiling with boost.thread in Qt Creator 0.9
    By Envergure in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2008, 07:10
  5. Compiling with boost.thread in Qt Creator 0.9
    By Envergure in forum Qt Programming
    Replies: 0
    Last Post: 28th November 2008, 03:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.