我正在为我当前的项目尝试一些 c++11 线程支持,但面临瓶颈。我使用 rubenvb 构建的 gcc 4.8.0(直接从 sourceforge 下载)[x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb],发现它似乎不支持 c++11 线程功能。以下代码无法编译并提示 'thread' is not member of 'std'
。通过谷歌搜索,有一个 4.7-experimental 构建(也是由 rubenvb 构建),具有 std 线程支持。由于我的主要项目的一部分需要 4.8 版本的其他 C++11 功能,我还没有尝试 4.7 版本。请帮助澄清我下载的工具链是否启用了线程。
平台:Window x64、Qt 4.8(使用下载的 gcc 工具链从源代码构建)
- 由于 Qt 库是从源代码构建的,更改工具链意味着重新编译整个库集,这对我来说听起来很糟糕。文件或 gcc 中是否遗漏了什么?谢谢。
更新:刚刚下载了实验性的4.8版本[x86_64-w64-mingw32-gcc-4.8-stdthread-win64_rubenvb]和编译好的测试代码。那么现在的问题是
“‘实验’版本使用安全吗?”
使用的测试代码
#include <iostream>
#include <thread>
#include <mutex> // try other c++11 threading feature
void doSomeWork( void ) { std::cout << "hello from thread..." << std::endl; }
int main() {
std::mutex m; // also fail here
std::thread t( doSomeWork );
t.join();
return 0;
}
和使用的makefile
CC := g++
SRC := test$(BUILD).cpp
OBJ := $(SRC:.cpp=.o)
BIN := test$(BUILD).exe
CXXFLAGS := -std=c++0x -Wall -Wextra -O0 -ggdb -lpthread -mthreads -pthread -Wno-unknown-pragmas
clean : ; del /a/s $(BIN) $(OBJ)
all : $(BIN) ; del $(OBJ)
$(BIN) : $(OBJ) ; $(CC) $(CXXFLAGS) $^ -o $@
%.o : %.cpp ; $(CC) -c $(CXXFLAGS) $< -o $@
.PHONY: clean all
最佳答案
Mingw-Builds 似乎有两种不同的线程模型,如果您使用的是 win32 模型:
Mingw-Builds (and the experimental rubenvb packages) also let you choose between the threading model internally used by (lib)gcc:
posix (built upon MinGW-w64’s winpthreads) “an implementation of POSIX threads for win32 is also available under the experimental directory.
- Its main goal is to support C++11 standard threading, which supports only using POSIX threads at the moment.” http://mingw-w64.sourceforge.net/
- enables C++11 library features contained in the headers thread, mutex, and future.
- Performance degradation in specific scenarios. C++11 functionality is significantly slower than native Win32 implementation or even MSVS2012’s implementation.
win32 uses native Win32 threading functions.
- no C++11 thread, mutex, or future
- best performance
关于c++ - 'thread' 不是 GCC 4.8 中 'std' 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951617/