nesca/Threader.cpp

37 lines
760 B
C++
Raw Normal View History

2015-03-22 00:43:15 +00:00
#include "Threader.h"
2015-03-17 14:30:53 +00:00
2015-03-19 14:34:35 +00:00
int Threader::threadId = 0;
2015-03-20 14:28:51 +00:00
std::mutex Threader::m;
bool Threader::ready = false;
std::condition_variable Threader::cv;
std::queue<std::string> Threader::ipQueue;
2015-03-17 14:30:53 +00:00
2015-03-20 14:28:51 +00:00
void Threader::fireThread(std::string ip, void *func(void)) {
2015-03-18 14:28:39 +00:00
2015-04-01 12:39:14 +00:00
ipQueue.push(ip);
2015-03-19 14:34:35 +00:00
if(threadId < gThreads) {
2015-03-20 14:28:51 +00:00
++threadId;
std::thread workerThread(func);
2015-03-19 14:34:35 +00:00
workerThread.detach();
}
2015-03-20 14:28:51 +00:00
2015-04-04 12:43:22 +00:00
ready = true;
cv.notify_one();
2015-03-20 14:28:51 +00:00
Sleep(gThreadDelay);
2015-03-18 14:28:39 +00:00
}
2015-03-17 14:30:53 +00:00
2015-03-19 14:34:35 +00:00
void Threader::cleanUp() {
2015-04-02 19:07:25 +00:00
ready = true;
2015-04-04 12:43:22 +00:00
cv.notify_all();
//Wait for threads to exit correctly
Sleep(gTimeOut*1000 + 2000);
2015-03-22 00:43:15 +00:00
std::unique_lock<std::mutex> lk(m);
lk.unlock();
lk.release();
2015-03-19 14:34:35 +00:00
threadId = 0;
2015-04-01 12:39:14 +00:00
std::queue<std::string> empty = {};
2015-03-28 05:30:41 +00:00
std::swap(ipQueue, empty);
2015-04-02 19:07:25 +00:00
ready = false;
2015-03-17 14:30:53 +00:00
}