C++ Logo

std-discussion

Advanced search

Re: CWG710 and races during object construction

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Fri, 12 Feb 2021 04:01:34 +0100
Hi,

Il 11/02/21 19:25, Jason Cobb via Std-Discussion ha scritto:
> Right, if it happened-before the end of the constructor (when not in the
> "Otherwise" case), [0] applies. But if it doesn't happen-before the end
> of the constructor, [1] still applies.
>
> (Disclaimer: I'm an idiot and this could all be very wrong.)
>
> [0]:https://eel.is/c++draft/class.cdtor#5.sentence-2
>
> [1]:https://eel.is/c++draft/basic.life#7.4

In other words, are you suggesting that the part about "For an object
under construction or destruction" is about establishing two
happens-before relationships -- beginning of construction happens-before
the usage of the glvalue, and such usage happens-before end of
construction -- without which we're falling into the "Otherwise" case?

(Sure, it's a possible reading; but then I'd claim that the text
should've also properly used "before" and "after" instead of "under", as
per the defect resolution...)


However, it begs further clarifications:

1) does "object" in the sentence refer to the complete object or to each
and every subobject being constructed? The entire section seems to refer
to complete objects, in fact Note 1 refers to [class.base.init] for
discussing the lifetime of subobjects.

> https://eel.is/c++draft/basic.life#3

If that's the case (complete object), then I'd say there's still a problem:


std::thread t;

struct B {
    B() { t = std::thread([&]{ typeid(*this); });
    virtual ~B();
};

struct D : B {
     D() { t.join(); }
} d;





2) does this also mean that this is UB?

struct S {
     S() { std::thread([&]{ this->f(); }).detach(); }
     void f();
} s;

We would be in the "Otherwise" case and triggering 7.2

> https://eel.is/c++draft/basic.life#7.2

?


Thanks,
-- 
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2021-02-11 21:01:42