apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robin J. Patenall" <Ro...@itvision.net>
Subject Cleaning up user objects containing threads.
Date Wed, 18 Oct 2006 15:59:18 GMT
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

Mime
View raw message