How EGCS with multi-threaded compliance?

Alexandre Oliva oliva@dcc.unicamp.br
Fri Apr 10 21:44:00 GMT 1998


Nathan Myers writes:

> Dirk Broer wrote:

>> I'm searching for a C++ compiler that is multi-thread compliance.

> Aren't we all...  An area most compilers haven't got right yet is

>   void f()
>   {
>     static A a;
>   }

> The constructor for 'a' is supposed to run the first time you enter f().

Actually, it is supposed to run the first time control passes through
the declaration-statement, unless the object is initialized together
with namespace-scope objects.

Since there's no requirement in the standard about making this
thread-safe, and, in fact, the C++ Standard does not talk about
thread-safety at all, I don't think compilers should care about this,
since it *is* possible to perform a similiar initialization in a
thread-safe manner:

static Mutex init_lock;
inline A& f_initialize_a()
{
  static A *a = 0;
  if (!a) {
    Lock lock(init_lock); // released when lock goes out of scope
    if (!a)
      a = new A();
  }
  return *a;
}

void f() {
  A& a = f_initialize_a();
}

Of course, a compiler might generate this code implicitly, but I don't 
think I would appreaciate that much hidden overhead.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil




More information about the Gcc mailing list