C++11 Std::Async Doesn't Work in Mingw

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

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/

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

Maybe it's easier for me to interpret because I wrote that code, but it's not so hard, just look at the first line of the error output:

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

That's telling you that std::future is an incomplete type, i.e. it is declared but not defined. The next line tells you exactly where it's declared (then all the other errors are caused by trying to use that incomplete type in different ways.)

If you look in GCC's <future> header you'll see that the types are declared near the top, but then the definitions are dependent on this preprocessor condition:

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

If you compile some simple tests with those macros you'll find out that _GLIBCXX_HAS_GTHREADS is not defined, and that's because your gcc -v shows

Thread model: win32

No one has provided the necessary code to make the C++11 thread features work on Windows yet.

Making <future> work would be harder, but it's not actually that difficult to enable <thread> and <mutex> but no one has stepped up to do the work yet. Yesterday I posted some ideas for how to enable the C++11 thread features for the win32 thread model: http://gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html

Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?

To use the MinGW-w64 with Win32 native threads you can install the mingw-std-threads headers.

As described on that page, this is because MinGW-w64 is a port of GCC, but GCC does not include any native thread support. Instead GCC installations typically implement threading via either gthreads or pthreads as a part of glibc. MinGW-w64 does not include a port of glibc. (Instead it uses a combination of the MSVC runtime, plus its own code to fill in holes).

Also as described on that page, recent versions of MinGW-w64 do include a Win32 port of pthreads ("winpthreads"), which explains why you can have threads work "out of the box" by selecting the "pthread" model from the MinGW-w64 installer.

std::stoi doesn't exist in g++ 4.6.1 on MinGW

This is a result of a non-standard declaration of vswprintf on Windows. The GNU Standard Library defines _GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can read more about this issue and macro here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.

If you're willing to modify the header files distributed with MinGW, you may be able to work around this by removing the !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of .../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding it back around lines 2905 to 2965 (the lines that reference std::vswprintf). You won't be able to use the std::to_wstring functions, but many of the other conversion functions should be available.



Related Topics



Leave a reply



Submit