mercredi 24 juillet 2019

How to create a thread by a child member function

I have a IBase class and a Child class. I need to call different proc function in different child class. I'm not sure which form below is actually right, maybe neither XD.

  • Form 1: Exactly I don't want my IBase have any non-virtual function.
  • Form 2: There's a strange expression &IBase::proc could make some misunderstanding.
class IBase
{
public:
    virtual void proc() = 0;

    auto createBind()
    {
        return bind(&IBase::proc, this);
    }
};
class Child :public IBase
{
public:
    void proc() override
    {
        cout << "Hello World" << endl;
    }
};
int main()
{
    IBase* pointer = new Child;

        //form 1
    thread th(pointer->createBind());
    th.join();

        //form 2
    thread th2(&IBase::proc, pointer);
    th2.join();

    cout << "Finish" << endl;
    return 0;
}


I'm wondering how do you guys solve this circumstance in a real project.

Aucun commentaire:

Enregistrer un commentaire