Search code examples
c++11gccpthreadsmingwstdasync

Coudn't run code used std::async on GCC 5.3.0


I was practicing std::asyn function that introduced in c++11, I wrote a simple example

#include<future>
#include<iostream>

using namespace std;

void check()
{
    cout<<"some"<<endl;
}
 int main()
 {
    auto p=std::async(std::launch::async,check);
    p.get();
 }

Yes very simple to start off with and i am compiling it using GCC 5.3.0

g++ -std=c++11 practise.cpp -lpthread

and the error

practise.cpp: In function 'int main()':
practise.cpp:13:47: error: invalid use of incomplete type 'class std::future<int>'
     auto p=std::async(std::launch::async,chech);
                                               ^
In file included from practise.cpp:1:0:
C:/Program Files/mingw32/i686-w64-mingw32/include/c++/future:115:11: note: declaration of 'class std::future<int>'
     class future;
           ^

Am i missing anything? is the way i linked lpthread is ok? i am on windows 7.


Solution

  • Your problem looks very similar to the one from this SO:

    c++11 std::async doesn't work in mingw

    You should check what gcc -v returns for 'Thread model:'. In above SO it returns win32 - and quite possibly mingw still does not support async/future in this mode.

    In my mingw installation - also 5.3.0, I have Thread model: posix. I checked the exact same compile flags as yours and your example always compiles fine.

    So my suggestion is for you to first check thread model with gcc -v, if its non posix, then reinstall mingw with posix threads. You choose threads model when running mingw-w64-install.exe installer/