[AGENT++] Agentpp::ThreadPool is idle and a task may be assigned to a not started thread in the pool?
Claus Klein
claus.klein at arcormail.de
Thu Sep 7 21:02:55 CEST 2017
Hi Frank,
I have a question about the Agentpp::ThreadPool:
void Thread::start()
{
if (status == IDLE) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, stackSize);
int err = pthread_create(&tid, &attr, thread_starter, this);
if (err) {
LOG_BEGIN(loggerModuleName, ERROR_LOG | 1);
LOG("Thread: cannot start thread (error)");
LOG(err);
LOG_END;
status = IDLE; <<<<< XXX why not FINISHED? CK
}
else {
status = RUNNING;
}
pthread_attr_destroy(&attr);
}
else {
LOG_BEGIN(loggerModuleName, ERROR_LOG | 1);
LOG("Thread: thread already running!");
LOG_END;
}
}
With this code, it may happen that not all threads of a ThreadPool could be created and started.
The ThreadPool is idle and a task may be assigned to a not started thread in the pool.
/**
* Check whether this thread is idle or not.
*
* @return
* TRUE if the thread managed by this TaskManager does
* not currently execute any task; FALSE otherwise.
*/
bool TaskManager::is_idle() { return (!task); }
bool TaskManager::set_task(Runnable *t)
{
lock();
if (!task) {
task = t;
notify();
unlock();
LOG_BEGIN(loggerModuleName, DEBUG_LOG | 2);
LOG("TaskManager: after notify");
LOG_END;
return TRUE;
}
else {
unlock();
LOG_BEGIN(loggerModuleName, DEBUG_LOG | 2);
LOG("TaskManager: got already a task");
LOG_END;
return FALSE;
}
}
The status is never checked by the TaskManager!
Is my understanding right?
How can this prevented?
With regards
Claus
More information about the AGENTPP
mailing list