I'm creating a thread pool system in C++ and I'm getting a weird exception when destroying all of my threads. This is what's happening:
terminate called without an active exception
This is the code for my Worker class:
Queue<Job> Worker::job_queue = Queue<Job>();
std::atomic<bool> Worker::run(false);
std::vector<Worker*> Worker::workers = std::vector<Worker*>();
std::mutex Worker::queue_mutex;
Worker::Worker() : worker_thread(Worker::loop, this){
worker_thread.detach();
workers.push_back(this);
}
void Worker::loop(){
while(Worker::run){
try{
queue_mutex.lock();
Job todo = job_queue.pop();
queue_mutex.unlock();
todo.job(todo.params);
todo.isDone = true;
} catch(...){
queue_mutex.unlock();
}
}
}
void Worker::init(){ //Static method; called when the program starts
run = true;
for(int i=0;i<NUM_OF_WORKERS;i++){
workers.push_back(new Worker());
}
}
void Worker::uninit(){ //Static method; called when the program is about to terminate
run = false;
for(int i=0;i<workers.size();i++){
delete workers[i];
}
}
Why is this happening and how can I fix it?