问题
I'm trying out some c++11 threading support for my current project but facing bottleneck. I use the gcc 4.8.0 built by rubenvb (downloaded directly from sourceforge) [x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb] and find it seems not support c++11 threading features. The following code fails to compile and complains 'thread' is not a member of 'std'
. With googling, there is a 4.7-experimental built (also by rubenvb) with std thread support. As part of my main project requires other C++11 features at 4.8 version, I haven't try out the 4.7 version. Please help to clarify if the toolchain I downloaded is thread-enabled or not.
Platform: Window x64, Qt 4.8 (built from source using the gcc tool-chain downloaded)
- As the Qt libaries are built from source, changing the tool-chains would mean recompile the whole library set, which sound bad to me. Are there anything missed in both files or gcc? Thanks.
Update: Just downloaded the experimental 4.8 version [x86_64-w64-mingw32-gcc-4.8-stdthread-win64_rubenvb] and the test code compiled. So the question now is
"Does the 'experimental' version safe to use?"
Test code used
#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;
}
and makefile used
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
回答1:
It seems that the Mingw-Builds have two different threading models, if you're using the win32 one:
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
Source: http://qt-project.org/wiki/MinGW-64-bit
来源:https://stackoverflow.com/questions/25951617/thread-is-not-a-member-of-std-in-gcc-4-8