vendredi 24 juillet 2015

How to execute method from class with std::thread [duplicate]

This question already has an answer here:

I have this constructor that initializes the bar member with 0 and starts a thread:

Foo::Foo() :
  bar(0)
{
   std::thread threadloop = std::thread(loop); //create a thread with the loop function
   threadloop.join();
}

And this loop() function from the Foo class:

void Foo::loop()
{
   while (true)
   {
      bar++;
      //do more stuff...
   }
}

The compiler complains that the loop method must be static... But if I put it static, how can I access the Foo private members that are not static? (and must not be static).

Aucun commentaire:

Enregistrer un commentaire