You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Cheng Po-wen <st...@itri.org.tw> on 2005/04/27 14:09:29 UTC

some questions about thread creation and thread pool

Hi,

I try to write a thread pool implementation on top of Apache APR,
I meet some problems:

1. apr_thread_create <group__apr__thread__proc.html#ga14> (apr_thread_t
<group__apr__thread__proc.html#ga2> **new_thread, ....., apr_pool_t
<group__apr__pools.html#ga0> *cont)

I've trace the source code about this function,
internally, it uses "count" to allocate the memory for apr_thread_t, &
pthread_t
like :

(*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
(*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));

and it uses "count" as parent pool to create a new pool for this thread,

when "child threads" exit, they will automatically destroy their own pool,
but the questions is this will still leave the apr_thread_t, & pthread_t
memory allocated in the parent pool "dangling".

so,....
can I solve the "dangling" memory problem when creating Thread Pool on
top of APR?
because threads are created & exit (if nessary) all the time in my
implementatin.

THANX

-- 
Sting, Cheng Po-wen(鄭博文)
Phone :  886-3-5914545  Fax : 886-3-5820085
E-Mail : sting@itri.org.tw

Internet Software Technology Division(W100)
Computer & Communication Research Lab. / ITRI


Re: some questions about thread creation and thread pool

Posted by Henry Jen <he...@ztune.net>.
On Wed, 2005-04-27 at 20:09 +0800, Cheng Po-wen wrote:
> Hi, 
> 
> I try to write a thread pool implementation on top of Apache APR,
> I meet some problems:
> 
> 1. apr_thread_create (apr_thread_t **new_thread, ....., apr_pool_t
> *cont)
> 
> I've trace the source code about this function,
> internally, it uses "count" to allocate the memory for  apr_thread_t,
> & pthread_t
> like :
> 
> (*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
> (*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));
> 
> and it uses "count" as parent pool to create a new pool for this
> thread,
> 
> when "child threads" exit, they will automatically destroy their own
> pool,
> but the questions is this will still leave the apr_thread_t, &
> pthread_t
> memory allocated in the parent pool "dangling".
> 

Automatically? Correct me if I am wrong, you need to call
apr_thread_exit explicitly within the thread function to destroy the
pool.

> 
> so,....
> can I solve the "dangling" memory problem when creating Thread Pool on
> top of APR?
> because threads are created & exit (if nessary) all the time in my
> implementatin.
> 

You have to destroy the pool you passed in to create the
thread(parameter cont) to reclaim the memory, I believe.

Cheers,
-- 
Henry Jen aka slowhog