1

So I've been trying to get the following code to compile and run on Windows by using a MinGW compiler.

#include <iostream>
#include <thread>
#include <future>

void ThreadWork(std::promise<int> &&p)
{
    // Pre-return from thread, do work after returning a value
    /* if else and things */ p.set_value(0);

    std::cout << "From thread" << std::endl;
    // More work
}

int main()
{
    std::promise<int> p;
    std::future<int> f = p.get_future();
    std::thread th(ThreadWork, std::move(p));

    // Get return value from thread
    int ret = f.get();
}

The above gives the error:

 error: aggregate 'std::promise<int> p' has incomplete type and cannot be defined
  75 |         std::promise<int> p;

error: variable 'std::future<int> f' has initializer but incomplete type
   76 |         std::future<int> f = p.get_future();

error: 'thread' is not a member of 'std'
   77 |         std::thread th(readInThread, callback, std::move(p));

Tried using official MinGW build, Tried using meganz/mingw-std-threads (that seem to throw errors like _WIN32_WININT is not set even though it is set to 0x0A00, if I try to set it manually compilation fails with redeclaration), and many more, succeed in none...

I also tried to link with pthreads: -lpthread no luck :(

Why are C++11 standard features still not implemented in MinGW? And what's the alternative for them currently?

Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
  • 1
    @TedLyngmo sorry I did added that, but still no luck, same error. – Animesh Sahu Jan 22 '21 at 12:51
  • To link `-lpthread` make sure it is installed . If you are using cmake try `find_package(Threads REQUIRED)` first – Umar Farooq Jan 22 '21 at 12:57
  • What compiler are you using? g++ 7.3.0 compiles this without errors. – TonyK Jan 22 '21 at 13:00
  • @TonyK g++ 9.2.0 @UmarFarooq doesn't symbols in `std::` namespace included in stdlib? – Animesh Sahu Jan 22 '21 at 13:02
  • You have some other problems in the code that may distract people from your actual problem. [demo](https://godbolt.org/z/rxY8Mx) - Also, if you ever need to link with `pthread`, use the `-pthread` option, not the `-lpthread` link instruction. – Ted Lyngmo Jan 22 '21 at 13:11

1 Answers1

0

futures and promises have been working for a long time in MinGW-w64. I am not content with the official binary distributions. Therefore I build MinGW-w64 on my own using https://github.com/niXman/mingw-builds.

With a Unix-like console (MSYS2) the steps after cloning the repository are the following:

cd /c/mingw-builds/
mkdir BuildRoot
git pull origin develop
git checkout develop
cd BuildRoot
../build --mode=gcc-10.2.0 --arch=x86_64 --buildroot=/c/mingw-builds/BuildRoot --update-sources --exceptions=seh --threads=posix --enable-languages=c++ --jobs=48 --rt-version=v7

#(https://stackoverflow.com/questions/32221221/mingw-x64-windows-plugin-needed-to-handle-lto-object)
mkdir mingw64/lib/bfd-plugins
cp mingw64/libexec/gcc/x86_64-w64-mingw32/10.2.0/liblto_plugin-0.dll mingw64/lib/bfd-plugins/liblto_plugin-0.dll

Then use the compiler, it should work like a charm and support futures and promises.

Benjamin Bihler
  • 1,612
  • 11
  • 32
  • The MSYS2 GCC packages should have std::thread support... Those are based on MinGW-w64 of course, not MinGW.org, which a far as I know, does not have support for a lot more than only std::thread. – rubenvb Jan 22 '21 at 13:14