You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by David Barrett <db...@quinthar.com> on 2004/09/28 07:36:13 UTC

Threads Best Practices

Hi there, I'm really starting to get into APR.  I was able to port to APR
mutexes without a hitch.  However, I'm a bit confused on threads, and I'd
appreciate any direction you can offer:
 
1) What should I do with "apr_threadattr_t"?  Need I create one, or can I
pass NULL for that field into "apr_thread_create"?  If I need to create one,
do I need to do anything with it, or can I use it with its default state?


2) What's the proper way to declare an "apr_start_thread_t" function?  Is
this what I'm supposed to do?

# void* ThreadProc( apr_thread_t* thread, void* data ) { }
# void foo( )
# {
#	...
#	apr_thread_create(
#		&thread, 
#		attr, 
#		(apr_thread_start_t)ThreadProc, 
#		(void*)data, 
#		pool
#	);
# }


3) What's the portable equivalent to Win32's "GetCurrentThreadId( )"?  I'd
like to associate some data with the thread, and then query it down the
road.  Currently I use "GetCurrentThreadId( )" in conjunction with my own
global map.  What I'd like to do us "apr_thread_data_get( )" to just query
the current thread's data, but it looks like I need to know the thread's
handle.  Is there any way to look up the handle for the current thread, or
query the data of the current thread without knowing its handle?


4) What's the proper way to clean up a thread?  I'm new to the whole "pool"
paradigm; do I simply destroy the pool I used to create the thread and its
attribute?  I assume I need to "apr_thread_join( )" on that thread first,
correct?


5) What's the portable equivalent to Win32's "Sleep( )" function?  I see
there is "apr_thread_yield( )", but that's not quite the same.  I'd like to
explicitly sleep the thread for some period; can APR do this?


Thanks for all your help, and the excellent library!

-david

PS: Is there any more-accessible form of the dev@apr.apache.org archives
than what's posted up on http://apr.apache.org/mail/dev/?  I'm finding it
difficult to search that effectively.  Alternatively, can you recommend any
better tools for searching the archives than "grep" and "vi"?  Like...
Google? :)