jeudi 4 janvier 2018

std::atomic

Does anybody know why this program will go in infinite loop , instead of stopping after 5s or so?

this happens with both gcc and clang latest compiler , is atomic_bool suffers from same issue as vector of bool?

as if I use atomic of int this works fine.

  #include <algorithm>                                                                                                                                                       
  #include <memory>                                                                                                                                                          
  #include <utility>                                                                                                                                                         
  #include <iostream>                                                                                                                                                        
  #include <vector>                                                                                                                                                          
  #include <functional>                                                                                                                                                      
  #include <future>                                                                                                                                                          
  #include <chrono>                                                                                                                                                          


  using namespace std;                                                                                                                                                       
  using namespace chrono_literals;                                                                                                                                           

  void send_heart_beat()                                                                                                                                                     
  {                                                                                                                                                                          
      cout << "sending heartbeat" << endl;                                                                                                                                   
  }                                                                                                                                                                          

  std::future<void> f;                                                                                                                                                       
  int main()                                                                                                                                                                 
  {                                                                                                                                                                          
     std::atomic<bool> stop(false);                                                                                                                                          
      f = std::async(std::launch::async,[&stop]() { while(!stop) { send_heart_beat(); std::this_thread::sleep_for(1s); } });                                                 
      std::this_thread::sleep_for(5s);                                                                                                                                       
      stop=true;                                                                                                                                                             
  }                  

Aucun commentaire:

Enregistrer un commentaire