[AGENT++] Agentpp::ThreadPool is idle and a task may be assigned to a not started thread in the pool?

Frank Fock fock at agentpp.com
Thu Sep 7 21:57:09 CEST 2017


Hi Claus,

Your analysis is correct. Thus, it is a bug which could lead to not executing a single task per non-started thread. It will not block the pool nor block subsequent tasks from being executed on running threads of the pool.

To fix this, the TaskManager::is_idle() method should read as follows in order to check if the associated thread is actually running:

	bool is_idle()	{ return (!task) && thread && thread.is_alive(); }

This will be part of the next release, which will be available next week.

Best regards,
Frank



> On 7. Sep 2017, at 21:02, Claus Klein <claus.klein at arcormail.de> wrote:
> 
> 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
> 
> 
> 
> _______________________________________________
> AGENTPP mailing list
> AGENTPP at agentpp.org
> https://oosnmp.net/mailman/listinfo/agentpp



More information about the AGENTPP mailing list