Actions

icon Post
text/html Subscribe
text/html Unsubscribe

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [c++-pthreads] Initialization of local static mutex


  • To: c++-pthreads@xxxxxxxxxxxxxxxx
  • Subject: Re: [c++-pthreads] Initialization of local static mutex
  • From: Jean-Marc Bourguet <jm.bourguet@xxxxxxxxx>
  • Date: Tue, 10 Oct 2006 08:05:16 +0200

Mark Mitchell wrote:
Roland Schwarz wrote:

Now that you have brought up this issue I am curious which "other
issues" you are refering to.

void f() {
  static int i = g();
}

Here, you cannot do the initialization statically; it must be done the first time that f() is called. So, the key question is whether the initialization is thread-safe. Does the programmer have to change that initialization to be thread-safe, using locks in f()? Or does the compiler take care of it?

I fail to see how the programmer can make the initialization thread-safe if the compiler doesn't take care of it? Lack of memory barriers could make the bool indicating that the initialization is done visible, but not the written value.

--
Jean-Marc