Actions
| Post | |
| Subscribe | |
| Unsubscribe |
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [c++-pthreads] Initialization of local static mutex
- To: jm@xxxxxxxxxxxx
- Subject: Re: [c++-pthreads] Initialization of local static mutex
- From: Mark Mitchell <mark@xxxxxxxxxxxxxxxx>
- Date: Tue, 10 Oct 2006 09:49:44 -0700
Jean-Marc Bourguet wrote:
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.
The programmer can't take care of it easily.
If the compiler doesn't do it for you, have to do things like:
static int i;
static bool initialized;
lock_mutex();
if (!initialized) {
i = g();
initialized = true;
}
unlock_mutex();
i.e., you have to manage the initialization manually. And, yes, there
are lots of problems with that, including exceptions being thrown by
g(). So, it's a good feature for compilers to have. Of course, since
it makes initialization slower (and code bigger) in the single-threaded
case, some programmers want to be able to turn off the compiler support.
-- Mark Mitchell CodeSourcery mark@xxxxxxxxxxxxxxxx (650) 331-3385 x713
- References:
- Initialization of local static mutex
- From: Roland Schwarz
- Re: [c++-pthreads] Initialization of local static mutex
- From: Mark Mitchell
- Re: [c++-pthreads] Initialization of local static mutex
- From: Roland Schwarz
- Re: [c++-pthreads] Initialization of local static mutex
- From: Mark Mitchell
- Re: [c++-pthreads] Initialization of local static mutex
- From: Roland Schwarz
- Re: [c++-pthreads] Initialization of local static mutex
- From: Mark Mitchell
- Re: [c++-pthreads] Initialization of local static mutex
- From: Roland Schwarz
- Re: [c++-pthreads] Initialization of local static mutex
- From: Mark Mitchell
- Re: [c++-pthreads] Initialization of local static mutex
- From: Jean-Marc Bourguet
- Initialization of local static mutex
- Prev by Date: Re: [c++-pthreads] Initialization of local static mutex
- Next by Date: Does the cancelation exception have a name?
- Previous by thread: Re: [c++-pthreads] Initialization of local static mutex
- Next by thread: Re: [c++-pthreads] Initialization of local static mutex
- Index(es):