无法在 GCC 5.3.0 上使用 运行 代码 std::async

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

我在练习std::asyn c++11中引入的函数,我写了一个简单的例子

#include<future>
#include<iostream>

using namespace std;

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

是的,开始非常简单,我正在使用 GCC 5.3.0 编译它

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

和错误

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;
           ^

我错过了什么吗?我链接 lpthread 的方式可以吗?我在 windows 7.

您的问题与此 SO 中的问题非常相似:

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

您应该检查 gcc -v returns 对 'Thread model:' 的影响。在上面所以它 returns win32 - 很可能 mingw 在这种模式下仍然不支持 async/future。

在我的 mingw 安装中 - 也是 5.3.0,我有 Thread model: posix。我检查了与你的完全相同的编译标志,你的示例总是编译正常。

所以我的建议是先用 gcc -v 检查线程模型,如果它不是 posix,然后用 posix 线程重新安装 mingw。 运行 mingw-w64-install.exe installer/

选择线程模型