我将Windows 8和MingW用作编译器套件。尝试安装
mingw32-libpthreadgc-dll
mingw32-libpthreadgc-dev
尝试执行
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g "C:\MinGW\bin\pthreadGC-3.dll" -c main.cpp -o obj\Debug\main.o
此
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthreadGC-3 -c main.cpp -o obj\Debug\main.o
此
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthreadGC -c main.cpp -o obj\Debug\main.o
还有这个
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthread -c main.cpp -o obj\Debug\main.o
每次都遇到相同的错误
error: 'thread' is not a member of 'std'
我的C:\MinGW\bin
包含pthreadGC-3.dll
,但不知道如何使用。
这是编译器的版本信息:
λ mingw32-g++ -v
Using built-in specs.
COLLECT_GCC=mingw32-g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
有人可以帮助我启用多线程吗?我认为我不需要共享代码,因为问题是一个通用的问题,但是如果有一些需要看的话,我将编辑问题。
编辑:这是代码:
#include <iostream>
#include <mutex>
#include <vector>
#include <thread>
using namespace std;
typedef std::function<void(void)> task;
class DispatchQueue {
public:
DispatchQueue(size_t thread_pool_size) {
std::cout << "Starting dispatch queue with " << thread_pool_size << " threads." << std::endl;
for (int i = 0; i < thread_pool_size; i++) {
threads_.emplace_back(std::thread([this]{task_executer();}));
}
}
~DispatchQueue() {
isStopped_ = true;
condVar_.notify_all();
std::cout << "Shutting down dispatch queue with " << threads_.size() << " threads." << std::endl;
for (std::thread& thread : threads_) {
thread.join();
}
std::cout << "Dispatch queue is shut down...";
}
void runAsync(const task& t) {
std::unique_lock<std::mutex> uniq_lock(mutLock_);
taskQueue_.push(t);
condVar_.notify_all();
}
private:
std::vector<std::thread> threads_;
std::queue<task> taskQueue_;
std::mutex mutLock_;
std::condition_variable condVar_;
bool isStopped_ = false;
int activeThreadCounter = 0;
void task_executer() {
std::unique_lock<std::mutex> uniq_lock(mutLock_);
while(!isStopped_ || taskQueue_.size() > 0) {
condVar_.wait(uniq_lock, [this]{
return taskQueue_.size() > 0 && isStopped_;
});
if (taskQueue_.size() > 0) {
std::cout << "Active threads: " << ++activeThreadCounter << std::endl;
const task t = std::move(taskQueue_.front());
taskQueue_.pop();
uniq_lock.unlock();
t();
uniq_lock.lock();
--activeThreadCounter;
}
}
}
};