You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by "Robin J. Patenall" <Ro...@itvision.net> on 2006/10/18 17:59:18 UTC

Cleaning up user objects containing threads.

Hi,

I'm trying to use APR on a project and I'm having some trouble with
cleaning up objects that I have which spawn their own worker thread.

The general plan is that I have an object that manages writing log
information to a file, this is created in a pool (call it A). This
spawns a worker thread, creating a child pool (call it B). I tried to
have a cleanup routine registered to pool A that when it was cleaned
would cleanly shutdown the object by telling the thread to terminate,
waiting for it and then returning.

The problem I found is that if I destroy or clean pool A the child pool
B is destroyed before the clean up routine in called. This means that
the worker thread is running with a destroyed pool and will destroy it
again when the thread terminates (causing a crash later on).  Even if I
wasn't using a thread, just a child object that is created in child
pool, there is no way a cleanup routine for the main object could safely
use the child object. This seems backwards from most object oriented
languages where child objects are only destructed after the parent
object so they can be used in the destructor.

My original thought was to attach the cleanup routine to the thread's
pool but this will still end up with the pool being destructed twice
(destroy pool B, which waits for the thread to finish. Thread destroys
the pool and finishes. Original pool gets destroyed again). The only way
I can see to solve this is with a WaitForAllThreadsToFinish function
that must be manually called before the pool gets destroyed which sort
of defeats the purpose of the cleanup function registration does it not?

Or have I missed something?

Robin Patenall