nesca/Threader.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

2015-03-17 14:30:53 +00:00
#include <Threader.h>
2015-03-18 14:28:39 +00:00
std::vector<char*> Threader::threadPool;
int Threader::threadPoolSize;
bool Threader::mayRun;
void *Threader::lFunc;
2015-03-17 14:30:53 +00:00
2015-03-18 14:28:39 +00:00
void Threader::recreateThreadPool(int poolSize) {
if(mayRun) {
createThreadPool(poolSize, (void* (*)(int))lFunc);
}
}
2015-03-17 14:30:53 +00:00
2015-03-18 14:28:39 +00:00
void Threader::createThreadPool(int poolSize, void *func(int)) {
cleanUp();
lFunc = (void*)func;
threadPoolSize = poolSize;
for(int i = 0; i < threadPoolSize; ++i) {
char *res = NULL;
threadPool.push_back(res);
std::thread thr12(func, i);
thr12.detach();
//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// _beginthread((void(*)(void*))_connect, 0, (void *)i);
//#else
// pthread_t thrc;
// pthread_create(&thrc, NULL, (void *(*)(void*))func, (void *)i);
//#endif
Sleep(1);
}
mayRun = true;
}
2015-03-17 14:30:53 +00:00
2015-03-18 14:28:39 +00:00
int Threader::getFreeDataSlotId() {
for(int i = 0; i != threadPoolSize; ++i) {
if(threadPool[i] == NULL) return i;
2015-03-17 14:30:53 +00:00
}
2015-03-18 14:28:39 +00:00
return -1;
}
int Threader::getFreeThreadId() {
int res;
while((res = getFreeDataSlotId()) < 0) Sleep(10);
return res;
2015-03-17 14:30:53 +00:00
}
2015-03-18 14:28:39 +00:00
void Threader::cleanUp() {
mayRun = false;
//for(int i = 0; i != threadPoolSize; ++i) {
//if(threadPool[i] != NULL) delete threadPool[i];
//}
threadPool.clear();
}
2015-03-17 14:30:53 +00:00
2015-03-18 14:28:39 +00:00
void Threader::fireThread(char *res) {
//while(!mayRun) sleep(10);
threadPool[getFreeThreadId()] = res;
2015-03-17 14:30:53 +00:00
}