Подтвердить что ты не робот

С++ 11 std:: async не работает в mingw

Запуск этого кода из презентации Herb Sutter . Это отлично работает в linux под gcc 4.6.3. Я думаю, что future.h не поддерживается в mingw, но ошибка действительно трудно понять!

#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]

Я использую 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)
4b9b3361

Ответ 1

Может быть, мне легче интерпретировать, потому что я написал этот код, но это не так сложно, просто посмотрите на первую строку вывода ошибки:

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

Никто не предоставил необходимый код, чтобы включить функции потока С++ 11 в Windows.

Выполнение работы <future> будет сложнее, но на самом деле это не так сложно включить <thread> и <mutex>, но никто еще не активизировал работу. Вчера я опубликовал некоторые идеи о том, как включить функции потока С++ 11 для модели потока win32: http://gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html

Ответ 2

Специальная сборка MinGW, называемая "MinGW builds", обеспечивает поддержку С++ 11 через gcc, включая async, thread, future и friends

(на этот раз пропущенный ответ IcemanX)

Он переместился на http://sourceforge.net/projects/mingwbuilds/

Как отмечает Джонатан Вакли (правильно и авторитетно), проблема с другими версиями GCC заключается в том, что они не обеспечивают поддержку pthreads, но MinGW-сборники. Решение, использующее потоки Windows, вероятно, будет лучше, но в настоящее время недоступно.

Я написал подробную инструкцию по установке и настройке для MinGW build + eclipse, здесь: http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds p >

Ответ 3

Не знаю, имеет ли std:: async действительную реализацию (если вообще), но std:: thread работает с версией MinGW/GCC 4.7, доступной на сайте проекта http://code.google.com/p/mingw-builds/

надеюсь, что это поможет

Ответ 4

Стандартная библиотека MinGW и компилятор не поддерживают std::async(*args)

Но есть модифицированная версия MinGW, которая имеет те же функции, что и оригинальная, которая: TDM-GCC MinGW

Это бесплатно для загрузки и регулярных обновлений. Кроме того, он поддерживает 32 и 64 бита также для компиляции кода.

Надеюсь, что это поможет ^^