mardi 25 juillet 2017

C++ thread get value for each loop

I recently start to learn c++, and now I'm playing with multithreading. What I am trying to do is to get the current value in a loop.

Like this :

std::atomic_int res(0);
void testThread(void){
  for(int i = 0; i < 5; i++)
    res += i;
}

int main(void){
  std::thread t(testThread);
  for(int i = 0; i < 5; i++){
    std::cerr << "res : " << res << std::endl;
  }
  t.join();
  return 0;
}

I know this is wrong but I don't know how to fix it... I tried to find how we do it, but I didn't find what I want.

Aucun commentaire:

Enregistrer un commentaire