I am using Windows 8 with MingW as compiler suit. Have tried installing
mingw32-libpthreadgc-dll
mingw32-libpthreadgc-dev
Tried executing this
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g "C:\MinGW\bin\pthreadGC-3.dll" -c main.cpp -o obj\Debug\main.o
This
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthreadGC-3 -c main.cpp -o obj\Debug\main.o
This
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthreadGC -c main.cpp -o obj\Debug\main.o
And this
λ mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -lpthread -c main.cpp -o obj\Debug\main.o
Getting same error everytime
error: 'thread' is not a member of 'std'
My C:\MinGW\bin
contains pthreadGC-3.dll
, but don't know how to use it.
Here is the version info of the compiler:
λ 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)
Could someone help me enable multithreading? I don't think I need to share the code, because the problem is a generic one, but if some needs to have a look at it, I will edit the question.
EDIT: Here is the code:
#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;
}
}
}
};