Advertisement
AntonioVillanueva

Temporizar un tiempo con chrono

Mar 1st, 2024 (edited)
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. //Contrôle du temps avec la bibliothèque chrono c++
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5.  
  6. #define TEMPO 4
  7. bool minutor(std::chrono::steady_clock::time_point debut, std::chrono::steady_clock::time_point& fin, std::chrono::seconds tempo) {
  8.    
  9.     std::chrono::seconds tempsEcoule = std::chrono::duration_cast<std::chrono::seconds>(fin - debut);
  10.  
  11.     if (tempsEcoule >= tempo) {
  12.         return true;
  13.     } else {
  14.         fin = std::chrono::steady_clock::now();
  15.     }
  16.  
  17.     return false;
  18. }
  19.  
  20. int main() {
  21.     std::chrono::steady_clock::time_point debut = std::chrono::steady_clock::now(); //debut t
  22.     std::chrono::steady_clock::time_point fin = std::chrono::steady_clock::now();//fin t
  23.     std::chrono::seconds tempo = std::chrono::seconds(TEMPO);//temps à contrôler
  24.  
  25.     while (true) {
  26.         std::this_thread::sleep_for(std::chrono::milliseconds());//sleep ms Un délai s'ajoute pour compliquer la situation
  27.        
  28.         if (minutor(debut, fin, tempo)) {
  29.             std::cout << "le temps est écoulé  " << std::endl;
  30.             return 0;
  31.         }
  32.     }
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement