mercredi 4 novembre 2015

How can a thread run a loop until joined?

Using std::thread to run a method which runs an infinite loop, is there a way the loop can query if the thread has been requested to join... or do I manually have to add a "exitThread" flag?

In other words what would isJoined look like (untested pseudo code):

std::atomic<int> global_counter (0);

void Run()
{
  while(!isJoined())
  {
    doSomething();
    ++global_counter;
  }
}

int main()
{
  thread t(Run);
  Sleep(10000);
  t.join();
  cout << "Iterated " << global_counter << "times" << endl;
}

Aucun commentaire:

Enregistrer un commentaire