This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: egcs@cygnus.com
- To: john dot bodenschatz at Aerojet dot com (Bodenschatz, John)
- Subject: Re: egcs@cygnus.com
- From: Alexandre Oliva <oliva at dcc dot unicamp dot br>
- Date: 16 Sep 1998 18:29:02 +-300
- Cc: "'egcs at cygnus dot com'" <egcs at cygnus dot com>
- References: <559F73FA94F9D111BF9D00805F1526671BA7DB@azu-mail2.aes.com>
Bodenschatz, John <john.bodenschatz@Aerojet.com> writes:
> When will the compiler be thread-safe for
> parallel processing?
Isn't it already? If you configure --enable-threads, the EH handling
code will be thread-safe. There's the issue of thread-safety in
initialization of local static variables, but, IMO, in order to be
portable, you should acquire a lock just before the potential
initialization of a local static variable, with a pattern as follows.
Instead of:
void foo() {
static bar baz = bar(/*baz_args*/);
...
}
write:
void foo() {
static bar* baz_ptr; // statically initialized to null
if (!baz_ptr) {
static mutex_t mutex; // must be statically initialized
lock mylock(mutex); // released when it goes out of scope
if (!baz_ptr) {
static bar baz = bar(/*baz_args*/);
baz_ptr = &baz;
}
}
static bar& baz = *baz_ptr;
...
}
Beautiful, isn't it? :-)
Of course, the compiler might do this for you, but, if you want
portable code, you'd better stick with this kind of code :-(
You might prefer to factor out the initialization of baz into a
separate inline function, but I'm not sure there's much to gain, and I
can't think of any way to do it in a template-based way :-(
There are also STL-related issues of thread-safety; the latest SGI
STL, distributed with the post-1.1 egcs snapshots, support a
compile-time option to become thread safe, but at a reasonably high
performance penalty. :-(
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil