[353] in Pthreads mailing list archive
Re: Fix to init pthreads earlier in C++
daemon@ATHENA.MIT.EDU (Rob Browning)
Sun Jul 7 04:03:42 1996
To: Ken Raeburn <raeburn@raeburn.org>
Cc: pthreads@MIT.EDU
From: Rob Browning <osiris@cs.utexas.edu>
Date: 07 Jul 1996 02:38:21 -0500
In-Reply-To: Ken Raeburn's message of 06 Jul 1996 23:52:28 -0400
Ken Raeburn <raeburn@raeburn.org> writes:
> It is safe to call pthread_init multiple times. How about this:
>
> struct __pthread_init {
> inline __pthread_init() {
> if (pthread_initial == 0)
> pthread_init ();
> }
> };
> static __pthread_init __pthread_init_this_file;
I'm not up on the details of statics from C files when mixed with
statics from C++ files. Are you sure that pthread_initial (since
global.c is compiled as a C, not C++ file) is guaranteed to be zeroed
*before* *any* of g++'s static initializers fire? If not, then this
is dangerous.
Your approach is probably OK, but unless ANSI guarantees the
semantics, it might not be as portable. Also bear in mind that it's
definitely not going to work if pthreads ever wants to have some
library cleanup in the future that gets executed whenever all program
code is finished using pthreads. In such a case, you'll have to have
the counter and the semaphore, but you probably knew that...
> inline __pthread_init() { pthread_init (); }
This brings up something that confuses me (it's late, I'm tired, so I
think I'm probably just being silly).
From Stroustroup and Ellis (p 19): "The initialization of nonlocal
static objects in a translation unit is done before the first use of
any function or object defined in that translation unit."
So say some static object's constructor calls functions in the same
translation unit. In such a situation it's impossible to guarantee
that all the static initializers will be called before any of the
functions since the static initializers themselves call the functions?
I'm probably just being too pedantic...
Anyway, I'm by no means saying that your approach won't work, just
that I don't know enough to be quite as sure that it will.
--
Rob