c++ - 'thread' 不是 GCC 4.8 中 'std' 的成员

标签 c++ c++11 stdthread

我正在为我当前的项目尝试一些 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

来源:http://qt-project.org/wiki/MinGW-64-bit

关于c++ - 'thread' 不是 GCC 4.8 中 'std' 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951617/

相关文章:

c++ - 使用变量类型的 std::function 运行线程

c++ - 为什么命令行参数的字符串比较不起作用?

c++ - 元组 std::get() 不适用于变量定义的常量

c++ - C++11 是否改变了显式调用 std::swap 以确保找到位于 ADL 的交换的行为,例如 boost::swap?

C++ std::thread 无效使用 void 表达式

c++ - 线程池棒

c# - 使用单一工具分析多种语言的日志文件。和日志记录框架的建议

c++ - 代码作为 C 文件工作,但不作为 C++ 文件工作,错误 : '__builtin_types_compatible_p' was not declared in this scope

c++ - 错误与否?带有 std::string 的 Visual Studio 2013 预览 std::vector 初始值设定项列表

c++ - 匹配成员函数存在和签名 : parameters