<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jun 9, 2014 at 12:00 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On Mon, Jun 9, 2014 at 11:44 AM, Zachary Turner <<a href="mailto:zturner@google.com">zturner@google.com</a>> wrote:<br>
</div><div class="">
> 2) If global_lock is a raw pointer to a mutex, it would have to be<br>
> explicitly allocated, and we can't guarantee this during static<br>
> initialization.<br>
<br>
</div>Can't guarantee that the memory allocation will succeed? But it could<br>
be done without a memory allocation - by placement new-ing into<br>
existing memory.</blockquote><div><br></div><div>It's more like you have nowhere to explicitly allocate it to guarantee that it will have been allocated by the time the other static object is trying to use it.  You can null-check it and lazily allocate, but this same null-check will be executed after main() enters, every time the ManagedStatic is accessed, and in that case it will be racy against whenever you shutdown and free the mutex.  Or, if someone did a refactor and removed the last instance of a ManagedStatic access from global static constructor, then the first access to any ManagedStatic would be racy.</div>
</div></div></div>