How to use thread sleep in C++

1 Answer

0 votes
#include <iostream>
#include <thread>
#include <chrono>

int main() {
    try {
        auto start = std::chrono::steady_clock::now();
        
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        
        auto end = std::chrono::steady_clock::now();
        
        std::cout << "Sleep time in ms = " 
                  << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() 
                  << std::endl;

    } catch (const std::exception& e) {
        std::cout << e.what() << std::endl;
    }
}


   
/*
run:
   
Sleep time in ms = 1000
   
*/

 



answered Sep 1, 2024 by avibootz

Related questions

1 answer 99 views
99 views asked Sep 1, 2024 by avibootz
1 answer 109 views
109 views asked Sep 1, 2024 by avibootz
1 answer 106 views
106 views asked Sep 1, 2024 by avibootz
4 answers 2,722 views
2,722 views asked Mar 24, 2021 by avibootz
...