You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Lucian Adrian Grijincu <lu...@gmail.com> on 2007/12/11 20:28:47 UTC

Proper thread termination management

a project I'm working on needs to create & destroy threads in a manner
that I is not under my control. I only supply it the thread creation
and termination routines.

If the main thread exits, it runs apr_terminate() and kills every
pool, including the thread pools (even if the threads are detachable).

To protect myself from SIGSEGVs I record each apr_thread_t* created
and join them all before finishing execution.

	create_thread
		apr_thread_create(&thd)
		list_add_joinable_thread(thd)

	before apr_terminate() I run something like this:
		foreach thd in joinable_thd_list do:
			apr_thread_join(&status, thd)

The list can grow much if the program runs for a long time and many
threads get created (and I end up holding many HANDLES/pthread_ts)

Now this could be fixed by creating another thread that polls on the
list and joins threads as soon as they are added to the list, but I
was wondering if any of you have tackled a similar problem and found a
better solution.


-- 
Lucian