English 中文(简体)
在一个基级建筑构造中制造的lam子,当叫起虚拟功能时,是否会在表面上引起数据竞赛?
原标题:Can a lambda created in a base class constructor cause a data race on the vtable when it calls a virtual function?

ipc 那么,《刑法》中是否有数据竞赛? 如果该表尚未完全建成,那么,则Mlambda可打电话Base:Handler(),但如果届时将打电话Derived:Handler(,右?

#include <thread>
#include <chrono>
#include <functional>
#include <iostream>
#include <mutex>
#include <condition_variable>

struct IPC
{
    IPC() : t([&]{
        std::unique_lock lk(mut);
        cv.wait(lk, [&]{ return (bool)f; });
        f();
    }) {}

    void SetHandler(std::function<void()> handler_) {
        {
            std::unique_lock lk(mut);
            f = handler_;
        }
        cv.notify_all();
    }

    std::mutex mut;
    std::condition_variable cv;
    std::function<void()> f;
    std::thread t;
};

struct Base {
    Base() {
        ipc.SetHandler([this]{ Handle(); });

        // Maybe construction of Base takes some time.
        // Removing this sleep makes the output of the program stabilize on "Derived".
        std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
    virtual void Handle() { std::cout << "Base" << std::endl; }
    IPC ipc;
};

struct Derived : Base {
    void Handle() override { std::cout << "Derived" << std::endl; }
};

int main()
{
    Derived b;
    // I only care about interaction with constructors so join here
    // to delay destruction of b until the test is finished.
    b.ipc.t.join();
}

Running this code multiple times in succession on my laptop produces randomly alternating outputs of "Base" and "Derived".

developer@WDX591VWD3:~$ g++-10 -std=c++20 hi.cpp -lpthread
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Derived
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Derived
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Derived
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Base
developer@WDX591VWD3:~$ ./a.out
Derived
developer@WDX591VWD3:~$

因此,我对问题持怀疑态度:这种不明确的行为吗? 我需要等待多长时间才能打电话给手,以便把衍生的班手提叫起来? 是否需要同步?

问题回答

That s going to have undefined behavior.

While I think that the standard currently lacks some wording to that effect, it is in my opinion intended that the object may be used in another thread only after the construction of the most-derived object has finished.

[qu.cdtor]/2,在试图通过一个并非从this的点子接触该物体时,也作了类似的规定。

I also expect that it isn t intended to matter whether or not the class is polymorphic. The restriction linked above also doesn t apply only to polymorphic classes.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签