Subject: libpthread and static constructor order
To: None <>
From: J.T. Conklin <>
List: tech-userlevel
Date: 11/01/2007 20:53:53
The pthread library's initialization function, pthread_init(), must be
called before any other pthread functions are called.  This is done by
using gcc's __constructor__ function attribute.  While this ensures it
will be called before main(), it's not enough if other static ctors
call pthread functions.

I'm running into this problem now with ACE / TAO, which has static
ctors which end up using pthread's thread specific storage.  If the
ACE / TAO ctors are executed before the libpthread ctor, bad things
happen.  If the libpthread ctor is executed before the ACE / TAO
ctors, everything works fine.  It's not clear to me what is needed to
make the libpthread ctors run first. I've been able to tweak the link
order by explicitly linking with -lpthread, but this is rather fragile.

Is there any way to ensure libpthread's ctors will be run before all
others?  ld.elf_so doesn't appear to grok the DF_1_INITFIRST flag
added by ld's -z initfirst option, and g++'s __init__priority__
function attribute doesn't work across shared libraries.

Without a reliable way to ensure libpthread's initialization is run
before all other static constructors, I think it needs to be changed
so that pthread_init() returns if it's already been initialized, and
something like:

   if (!__isthreaded) {

be added to the beginning of some (all?, most?) of the pthread calls.
Are there any other options?


J.T. Conklin