簡體   English   中英

c ++ 11 std::async 在 mingw 中不起作用

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

從 Herb Sutter 的演示文稿中運行此代碼。 這在 gcc 4.6.3 下的 linux 中工作正常。 我認為 mingw 不支持 future.h,但錯誤真的很難理解!

#include <iostream>
#include <vector>
#include <string>
#include <future>
#include <algorithm>

using namespace std;

string flip( string s ) {
  reverse( begin(s), end(s) );
  return s;
}

int main() {
  vector<future<string>> v;

  v.push_back( async([]{ return flip(" ,olleH"); }) );
  v.push_back( async([]{ return flip(".gnaL"); }) );
  v.push_back( async([]{ return flip("\n!TXEN"); }) );

  for( auto& e: v ) {
    cout << e.get();
  }
}

這是錯誤:

$ x86_64-w64-mingw32-g++.exe -std=c++0x -pthread swap.cpp
swap.cpp: In function 'int main()':
swap.cpp:17:51: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:18:49: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:19:51: error: invalid use of incomplete type 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'std::__async_sfinae_helper<main()::<lambda()>, main()::<lambda()> >::type {aka class std::future<std::basic_string<char> >}'
swap.cpp:22:14: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'class std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_algobase.h:68:0,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/char_traits.h:41,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/ios:41,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/ostream:40,
                 from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/iostream:40,
                 from swap.cpp:1:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_iterator.h: In instantiation of '__gnu_cxx::__normal_iterator<_Iterator, _Container>& __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator++() [with _Iterator = std::future<std::basic_string<char> >*; _Container = std::vector<std::future<std::basic_string<char> > >; __gnu_cxx::__normal_iterator<_Iterator, _Container> = __gnu_cxx::__normal_iterator<std::future<std::basic_string<char> >*, std::vector<std::future<std::basic_string<char> > > >]':
swap.cpp:21:17:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_iterator.h:750:2: error: cannot increment a pointer to incomplete type 'std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/vector:65:0,
                 from swap.cpp:2:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]':
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:247:15:   required from 'std::vector<_Tp, _Alloc>::vector() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]'
swap.cpp:15:26:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:161:9: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of 'class std::future<std::basic_string<char> >'
In file included from c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/vector:63:0,
                 from swap.cpp:2:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::future<std::basic_string<char> >*]':
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h:155:7:   required from 'void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _ForwardIterator = std::future<std::basic_string<char> >*; _Tp = std::future<std::basic_string<char> >]'
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_vector.h:403:9:   required from 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = std::future<std::basic_string<char> >; _Alloc = std::allocator<std::future<std::basic_string<char> > >]'
swap.cpp:15:26:   required from here
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/bits/stl_construct.h:128:7: error: invalid use of incomplete type '_Value_type {aka class std::future<std::basic_string<char> >}'
In file included from swap.cpp:4:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:111:11: error: declaration of '_Value_type {aka class std::future<std::basic_string<char> >}'
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future: At global scope:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include/c++/future:187:5: error: 'typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...) [with _Fn = main()::<lambda()>; _Args = {}; typename std::__async_sfinae_helper<typename std::decay<_Func>::type, _Fn, _Args ...>::type = std::future<std::basic_string<char> >]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]

我在 Windows 中使用 GCC 4.7。

$ g++ -v
Using built-in specs.
COLLECT_GCC=c:\mingw64\bin\x86_64-w64-mingw32-g++.exe
COLLECT_LTO_WRAPPER=c:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-4.7.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --disable-multilib --enable-64bit --prefix=/mingw64 --with-sysroot=/mingw64 --disable-shared --enable-static --disable-nls --enable-version-specific-runtime-libs --disable-win32-registry --without-dwarf2 --enable-sjlj-exceptions --enable-fully-dynamic-string --enable-languages=c,ada,lto,c++,objc,obj-c++,fortran --enable-libgomp --enable-lto --enable-libssp -enable-gnattools --disable-bootstrap --with-gcc --with-gnu-as --with-gnu-ld --with-stabs --enable-interwork --with-mpfr-include=/home/beta/gcc-build/../gcc-4.7.0/mpfr/src --with-mpfr-lib=/home/beta/gcc-build/mpfr/src/.libs
Thread model: win32
gcc version 4.7.0 (GCC)

也許因為我寫了那段代碼,我更容易理解,但也沒有那么難,看看錯誤輸出的第一行:

swap.cpp:22:14: error: invalid use of incomplete type 'class std::future<std::basic_string<char> >'

這告訴你std::future是一個不完整的類型,即它被聲明但沒有定義。 下一行告訴您它的確切聲明位置(然后所有其他錯誤都是由嘗試以不同方式使用該不完整類型引起的。)

如果您查看 GCC 的<future>標頭,您會看到類型聲明在頂部附近,但定義取決於此預處理器條件:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
  && (ATOMIC_INT_LOCK_FREE > 1)

如果您使用這些宏編譯一些簡單的測試,您會發現_GLIBCXX_HAS_GTHREADS未定義,這是因為您的gcc -v顯示

Thread model: win32

沒有人提供必要的代碼來使 C++11 線程功能在 Windows 上工作。

<future>工作會更難,但實際上啟用<thread><mutex>並沒有那么難,但還沒有人加緊工作。 昨天我發布了一些關於如何為 win32 線程模型啟用 C++11 線程功能的想法: http : //gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html

名為“MinGW builds”的特殊 MinGW 構建確實通過 gcc 提供了 C++11 支持,包括asyncthreadfuture和朋友

(這秒了 IcemanX 投反對票的答案)

它已移至http://sourceforge.net/projects/mingwbuilds/

正如 Jonathan Wakely 指出(正確且權威地)其他版本的 GCC 的問題在於它們不提供對pthreads 的支持,但 MinGW-builds 提供。 使用 Windows 線程的解決方案可能會更好,但目前不可用。

為 MinGW build+eclipse寫了詳細的安裝和設置說明,在這里: http : //scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds

不知道 std::async 是否具有有效的實現(如果有的話)但 std::thread 與 MinGW/GCC 4.7 版本一起使用,可在項目站點http://code.google.com/p/mingw-builds /

希望這可以幫助

標准 MinGW 庫和編譯器不支持std::async(*args)

但是有一個修改版的 MinGW 和原來的一樣,就是: TDM-GCC MinGW

它可以免費下載並定期更新。 此外,它還支持32 位和 64 位編譯代碼。

希望有幫助^^

只需從此處下載 MinGW 安裝程序並使用配置 posix 線程進行安裝。

在此處輸入圖片說明

如果您已經安裝了舊的 MinGW,請編輯您的環境變量,以便使用新安裝的 MinGW。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM