Cross platform Sleep function for C++

后端 未结 7 1451
花落未央
花落未央 2020-12-05 04:28

Is it possible with macros make cross platform Sleep code? For example

#ifdef LINUX
#include 
#endif
#ifdef WINDOWS
         


        
7条回答
  •  情书的邮戳
    2020-12-05 05:12

    since c++ 11 you could just do this.

    #include
    #include
    int main(){
        std::this_thread::sleep_for(std::chrono::milliseconds(x));//sleeps for x milliseconds
        std::this_thread::sleep_for(std::chrono::seconds(x));//sleeps for x seconds  
        std::this_thread::sleep_for(std::chrono::minutes(x));//sleeps for x minutes
        std::this_thread::sleep_for(std::chrono::hours(x));//sleeps for x hours.
      return 0;  
    }
    

    I don't know why would you want to use messy macros when you can do this, this method is great, cross platform and is included in the c++ standard.

提交回复
热议问题