如何使用 G++ 在 windows (MinGW - W64) 中使用 #include <thread> 编译 C++ 应用程序?

问题描述

我曾尝试使用 G++ 用 #include <thread> 编译我的 C++ 文件,但它失败了。这是我的源代码 下面:

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;

void act_1()
{
    cout << "Hello World from act_1\n";
    return;
}

int main()
{
        thread th(act_1);
        th.join();
        
        ExitProcess(EXIT_SUCCESS);
}

现在,我使用了标志 -std=c++11 和 -pthreads,但它仍然无法正常工作。这是控制台输出

C:\Users\Mayukh\Desktop>g++ -std=c++11 -pthread threaded.cpp -o thread
threaded.cpp: In function 'int main()':
threaded.cpp:15:3: error: 'thread' was not declared in this scope
   thread th(act_1);
   ^~~~~~
threaded.cpp:15:3: note: 'std::thread' is defined in header '<thread>'; did you
forget to '#include <thread>'?
threaded.cpp:4:1:
+#include <thread>

threaded.cpp:15:3:
   thread th(act_1);
   ^~~~~~
threaded.cpp:16:3: error: 'th' was not declared in this scope
   th.join();
   ^~
threaded.cpp:16:3: note: suggested alternative: 'tm'
   th.join();
   ^~
   tm

请帮帮我

解决方法

我现在找到了答案,因为我使用的是 x86_x64_win32,并且它没有实现线程,所以我切换到 x86_x64_posix,它现在可以工作了。

如果有人仍然可以在 MinGW - W64 的 x86_x64_win32 中找到解决方案,请发布答案