Does a member member flow affect other members of its parent class in the parent destructor and cause undefined behavior?

One of my colleagues claims that as soon as the call to the object's destructor begins, all calls to the members of the object made by the thread (which is a member of the object itself) are UB.

This means that the call std::thread::joinduring the destructor of the object is UB if the thread accesses any of the objects of other members.

I briefly reviewed the last standard project in the “Object of Life” section, but could not find anything that gave me the final answer.

Does the following code (on wandbox) introduce undefined behavior? . What part of the standard clarifies this interaction?

struct A 
{
    atomic<bool> x{true};
    thread t;

// Capturing 'this' is part of the issue.
// The idea is that accessing 'this->x' becomes invalid as soon as '~A()' is entered.
//           vvvv
    A() : t([this]
            { 
                while(x) 
                {
                    this_thread::sleep_for(chrono::milliseconds(100)); 
                }
            }) 
    { 
    }

    ~A() 
    { 
        x = false; 
        t.join(); 
    }
};

int main()
{
    A a;
}
+4
2

undefined. [class.dtor]/8,

, , X Xs, Xs , X - (12.6.2), Xs.

, , . , x, join , -. , , , .

+6

N3337, ++ 11.

, undefined :

§12.7 [class.cdtor]/1

, undefined.

,

§12.4 [class.dtor]/15

,

12.7 :

§3.8 [basic.life]/5

, , 38 , , , , , , . , . 12.7.

+3

Source: https://habr.com/ru/post/1659731/


All Articles