workPool: handle thread init failures

This commit is contained in:
tildearrow 2023-09-07 01:16:12 -05:00
parent 1a520dbf3e
commit 157f3be253
2 changed files with 20 additions and 4 deletions

View file

@ -100,9 +100,16 @@ void DivWorkThread::finish() {
thread->join(); thread->join();
} }
void DivWorkThread::init(DivWorkPool* p) { bool DivWorkThread::init(DivWorkPool* p) {
parent=p; parent=p;
try {
thread=new std::thread(_workThread,this); thread=new std::thread(_workThread,this);
} catch (std::system_error& e) {
logE("could not start thread! %s",e.what());
thread=NULL;
return false;
}
return true;
} }
void DivWorkPool::push(void (*what)(void*), void* arg) { void DivWorkPool::push(void (*what)(void*), void* arg) {
@ -171,7 +178,16 @@ DivWorkPool::DivWorkPool(unsigned int threads):
if (threaded) { if (threaded) {
workThreads=new DivWorkThread[threads]; workThreads=new DivWorkThread[threads];
for (unsigned int i=0; i<count; i++) { for (unsigned int i=0; i<count; i++) {
workThreads[i].init(this); if (!workThreads[i].init(this)) {
count=i;
break;
}
}
if (count<=0) {
logE("DivWorkPool: couldn't start any threads! falling back to non-threaded mode.");
delete[] workThreads;
threaded=false;
workThreads=NULL;
} }
} else { } else {
workThreads=NULL; workThreads=NULL;

View file

@ -57,7 +57,7 @@ struct DivWorkThread {
bool busy(); bool busy();
void finish(); void finish();
void init(DivWorkPool* p); bool init(DivWorkPool* p);
DivWorkThread(): DivWorkThread():
parent(NULL), parent(NULL),
isBusy(false), isBusy(false),