<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 710</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="710"></A><H4>710.
  
Data races during construction
</H4>
<B>Section: </B>11.9.5&#160; [<A href="https://wg21.link/class.cdtor">class.cdtor</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD2
 &#160;&#160;&#160;

 <B>Submitter: </B>Jeffrey Yasskin
 &#160;&#160;&#160;

 <B>Date: </B>3 May, 2008<BR>


<P>[Voted into WP at March, 2010 meeting.]</P>

<P>Consider the following example:</P>

<PRE>
    struct A {
      A() {
        std::thread(&amp;A::Func, this).detach();
      }
      virtual void Func() {
        printf("In A");
      }
    };

    struct B : public A {
      virtual void Func() {
        printf("In B");
      }
    };

    struct C : public B {
      virtual void Func() {
        printf("In C");
      }
    };

    C c;
</PRE>

<P>What is the program allowed to print?  Should it be undefined
behavior or merely unspecified which of the <TT>Func()</TT>s is
called?</P>

<P>There is a related question about which variables <TT>C::Func()</TT>
can depend on having been constructed.  Unless we want to require
the equivalent of at least <TT>memory_order_consume</TT> on the
presumed virtual function table pointer, I think the answer is just
the members of <TT>A</TT>.</P>

<P>If I instead just have</P>

<PRE>
    A a;
</PRE>

<P>I think the only reasonable behavior is to print <TT>In A</TT>.</P>

<P>Finally, given</P>

<PRE>
    struct F {
      F() {
        std::thread(&amp;F::Func, this).detach();
      }
      virtual void Func() {
        print("In F");
      }
    };

    struct G : public F {
    };

    G g;
</PRE>

<P>I can see the behavior being undefined, but I think a lot of
people would be confused if it did anything other than print
<TT>In F</TT>.</P>

<P>Suggested resolution:</P>

<P>I think the intent here is that an object should not be used in
another thread until any non-trivial constructor has been called.
One possible way of saying that would be to add a new paragraph
at the end of 11.9.5 [<A href="https://wg21.link/class.cdtor">class.cdtor</A>]:</P>

<BLOCKQUOTE>

A constructor for a class with virtual functions or virtual
base classes modifies a memory location in the object that is
accessed by any access to a virtual function or virtual base
class or by a <TT>dynamic_cast</TT>.  [<I>Note:</I> This implies
that access to an object by another thread while it is being
constructed often introduces a data race (see
6.10.2 [<A href="https://wg21.link/intro.multithread">intro.multithread</A>]). &#8212;<I>end note</I>]

</BLOCKQUOTE>

<P><B>Proposed resolution (October, 2009):</B></P>

<P>Add the following as a new paragraph at the end of 6.8.4 [<A href="https://wg21.link/basic.life">basic.life</A>]:</P>

<BLOCKQUOTE>

<INS>In this section, &#8220;before&#8221; and &#8220;after&#8221; refer to
the &#8220;happens before&#8221; relation (6.10.2 [<A href="https://wg21.link/intro.multithread">intro.multithread</A>]). [<I>Note:</I> Therefore, undefined behavior results if
an object that is being constructed in one thread is referenced from a
different thread without adequate synchronization. &#8212;<I>end
note</I>]</INS>

</BLOCKQUOTE>

<BR><BR>
</BODY>
</HTML>
