How to enable experimental C++11 concurrency features in MinGW?

前端 未结 8 1948
夕颜
夕颜 2020-11-30 06:46

When trying to compile the following code

#include 
#include 

void foo() { std::cout << \"foo\\n\"; }

int main()
{
  st         


        
相关标签:
8条回答
  • 2020-11-30 07:08

    When you get a compiler that supports std::thread here is your corrected example (two minor type-o's):

    #include <thread>
    #include <iostream>
    
    void foo() { std::cout << "foo\n"; }
    
    int main()
    {
      std::thread t(foo);
      t.join();
    }
    
    0 讨论(0)
  • 2020-11-30 07:14

    There is already a lightweight native implementation of std::threads and sync primitives: https://github.com/meganz/mingw-std-threads

    It is a header-only library and it should work with any C++11 version of MinGW.

    0 讨论(0)
  • 2020-11-30 07:21

    To the best of my knowledge, MinGW does not support yet the new c++0x concurrency features (as of GCC 4.5). I remember reading a mailing list exchange in which it was pointed out that in MinGW the following ifdef from the thread header is not satisfied:

    #if defined(_GLIBCXX_HAS_GTHREADS)
    

    I guess this is somehow related to the way MinGW is built under Windows, whether it uses native threads or pthread, etc. In my code, I've written some minimal wrapping that uses Boost.thread instead of native c++0x threads when in Windows. The two interfaces are very similar and for many uses they can be swapped without issues.

    EDIT: Thanks to Luc Danton for digging out the mailing list thread mentioned above:

    http://comments.gmane.org/gmane.comp.gnu.mingw.user/33065

    0 讨论(0)
  • 2020-11-30 07:24

    There is an another option.

    //Threading01.cpp
    
    #include <thread>
    #include <iostream>
    
    void hello()
    {
        std::cout<< "Hello Threading ..." << std::endl;
    }
    
    int main()
    {
        std::thread t1(hello);
        t1.join();
    
        return 0;
    }
    

    Download mingw-w64 (The mingw-w64 project on sourceforge.net is moving to mingw-w64.org) and execute the .bat file(mingw-w64.bat) they provided.In the provided command line, you can execute your thread code like this

    C:\CPP>g++ std=c++11 -g -Wall -lpthread -o Threading01 Threading01.cpp
    
    0 讨论(0)
  • 2020-11-30 07:28

    As others have mentioned, the mingw port of gcc does not provide C++0x concurrency support out of the box. However, the commercial just::thread library provides these facilities, so you can use std::thread with the TDM/mingw port of gcc 4.5.2.

    Disclaimer: I am the primary developer of the just::thread library.

    0 讨论(0)
  • 2020-11-30 07:29

    Try MinGw builds:

    http://sourceforge.net/projects/mingwbuilds/

    This installer will allow you to choose whatever MinGW you want and also includes c++11 thread functionality.

    0 讨论(0)
提交回复
热议问题