On 21 May 2011 17:05, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div>On May 19, 2011, at 11:11 PM, Nick Lewycky wrote:<br>
> I've started dipping my toes in the libcxxabi project by implementing a few simple methods. Patch attached, please review!<br>
><br>
> This isn't heavily tested, I'm mostly just trying to make sure I have the style down. Please let me know if we should be using different file names (hard to follow a pattern where there isn't one), of if the private methods in cxa_guard actually belong in an anonymous namespace, etc.<br>






<br>
</div>Guard variables need to support recursive initialization:  the initialization<br>
of one variable must be able to kick off arbitrary code that might perform<br>
its own initialization.</blockquote><div><br></div><div>Okay, I can handle that. I wrote a test for it:</div><div><br></div><div>namespace test3 {</div><div>    int helper1(int i) {</div><div>        if (i == 0)</div><div>

            return 0;</div><div>        static int j = 0 helper1(i - 1);</div><div>        return j + 1;</div><div>    }</div><div>    void test() {</div><div>        static int x = helper1(10);</div><div>    }</div><div>

}</div><div><br></div><div>and discovered that libstdc++ doesn't support it. They detect the recursive initialization and throw an exception.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

In C++11, there's a guarantee that this won't deadlock<br>

even if the first initialization blocks on a different thread.<br></blockquote><div><br></div><div>Hold up, what does this actually mean? Given:</div><div><br></div><div>  int test_a(int x) {</div><div>    if (x == 0)</div>


<div>      return 0;</div><div>    static int A = test_b(x - 1);</div><div>    return A + 1;</div><div>  }</div><div>  int test_b(int x) {</div><div><div>    if (x == 0)</div>
<div>      return 0;</div><div>    static int B = test_a(x - 1);</div><div>    return B + 1;</div><div>  }</div></div><div><br></div><div>suppose that thread 1 enters test_a(10) and grabs the lock on A at the same time that thread 2 enters test_b(10) and grabs the lock on B. The deadlock is avoided how?</div>


<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Probably the most reasonable way to handle this is to have a global lock<br>
which is *not* held during initialization, and a global condition variable<br>
that threads wait on if they're waiting for somebody else to perform an<br>
initialization.<br></blockquote><div><br></div><div>It's not clear to me what you're proposing, but I strongly want to avoid anything that blocks the other threads from running their own initializers in parallel for as long as they're independent. I think I'll understand your proposal once I understand the constraints you mentioned above.</div>

<div><br></div><div>Nick</div><div><br></div></div>