【问题标题】:Why is there still no std::thread, std::promise and std::future in MinGW? And what's the alternative to promise and futures in win32?为什么 MinGW 中仍然没有 std::thread、std::promise 和 std::future? win32中promise和futures的替代方案是什么?
【发布时间】:2021-01-22 12:41:47
【问题描述】:

所以我一直在尝试使用 MinGW 编译器让以下代码在 Windows 上编译和运行。

#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();
}

上面给出了错误:

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

尝试使用官方 MinGW 构建,尝试使用 meganz/mingw-std-threads(即使设置为 0x0A00,似乎也未设置 _WIN32_WININT 之类的错误,如果我尝试手动设置编译失败并重新声明),还有很多更多,没有成功...

我还尝试与 pthread 链接:-lpthread 不走运 :(

为什么 C++11 标准功能仍未在 MinGW 中实现?他们目前的替代方案是什么?

【问题讨论】:

  • @TedLyngmo 对不起,我确实添加了,但仍然没有运气,同样的错误。
  • 要链接-lpthread,请确保已安装。如果您使用的是 cmake,请先尝试find_package(Threads REQUIRED)
  • 你用的是什么编译器? g++ 7.3.0 编译这个没有错误。
  • @TonyK g++ 9.2.0 @UmarFarooq ???? std:: 命名空间中的符号不​​包含在 stdlib 中吗?
  • 代码中还有一些其他问题可能会分散人们对实际问题的注意力。 demo - 此外,如果您需要与 pthread 链接,请使用 -pthread 选项,而不是 -lpthread 链接指令。

标签: c++ windows multithreading c++11 mingw


【解决方案1】:

futures 和 promises 在 MinGW-w64 中工作了很长时间。我不满足于官方的二进制发行版。因此,我使用 https://github.com/niXman/mingw-builds 自己构建 MinGW-w64。

使用类 Unix 控制台 (MSYS2) 克隆存储库后的步骤如下:

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

然后使用编译器,它应该像魅力一样工作并支持futures 和promises。

【讨论】:

  • MSYS2 GCC 包应该有 std::thread 支持......当然,这些是基于 MinGW-w64,而不是 MinGW.org,据我所知,它不支持不仅仅是 std::thread。
猜你喜欢
  • 2016-03-14
  • 2014-11-10
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2020-12-29
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多