This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: egcs@cygnus.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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]