Re: [c++-pthreads] Initialization of local static mutex
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



Roland Schwarz wrote:

"The zero-initialization of all local objects with static
storage duration is performed before any other initialization
takes place. A local object of POD type with static storage
duration initialized with constant-expressions is initialized
before its block is first entered. ..."

Yes, I've read this. (I've spent more than a decade working on C++ compilers, the C++ standard, and C++ ABIs.)

Does this mean, the compiler is allowed to emit code to initialize
the mutex variable at runtime, after "any other initializations
have taken place" and before "block is entered"?

Yes.

A compiler is still conforming in this case, while at the same
time susceptible to the race.

No. If the initialization occurs before the block is entered, there is no race, independent of when and how that initialization occurs. The race can only occur inside the block.

In some sense, the question you're asking has no well-formed answer. The C++ standard doesn't mention threads; therefore, it can't talk about thread-safety. Therefore, if you need to formally validate of your program that it is thread-safe, you're going to need look beyond the C++ stnadard, or even the POSIX standard; you're going to need to define your own model.

However, in practice, your code is safe. There are other issues around thread-safe initialization of statics with a constant initializers which are addressed by some C++ ABIs, but you have the simpler case of a constant initializer.

--
Mark Mitchell
CodeSourcery
mark@xxxxxxxxxxxxxxxx
(650) 331-3385 x713