mardi 21 avril 2020

std::bind a thread memeber variable to an instance of a class

I am confused between the various ways I can create member threads to run on member functions of a class instance and what are the differences between all of them :- First method - using lambda expressions

auto m_thread = std::thread([this]{run();});

Second method

auto m_thread = std::thread(std::bind(&MyType::run, this));

Third method

auto res = std::bind(&m_thread, std::bind(&MyType::run, this));

Fourth method -

auto res = std::bind(&m_thread, &MyType::run, this);

Here, m_thread is a member variable of a Class MyType given by std::thread m_thread of which this is an instance and run is a member function of the same class. Will all of these give the same results and are they equivalent? Also, in the last two cases how to make the thread start executing.

Aucun commentaire:

Enregistrer un commentaire