Several methods of delay processing for C + + program

Source: Internet
Author: User

(-) using the _sleep () function

Example: _sleep (200);//DELAY 200 ms

(ii) using the delay (int time) function (which requires its own implementation, not within the compiler)

[CPP]View PlainCopy
  1. @brief Program delay
  2. @param [in] msec: MS
  3. @remark
  4. @return void
  5. void delay_msec (int msec)
  6. {
  7. clock_t now = clock ();
  8. while (clock ()-now < msec);
  9. }
  10. @brief Program delay
  11. @param [In] sec: sec
  12. @remark
  13. @return void
  14. void Delay_sec (int sec)//
  15. {
  16. time_t start_time, Cur_time;
  17. Time (&start_time);
  18. Do
  19. {
  20. Time (&cur_time);
  21. } while ((Cur_time-start_time) < sec);
  22. }

For example, the delay of 2 seconds can be this: Delay_msec (2000); or delay_sec (2);

It should be noted that DELAY_MSEC has a higher time accuracy (up to 1 milliseconds, depending on the compiler definition) because it uses the clock ().

51943581

Several methods of delay processing for C + + program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.