Home > Backend Development > C++ > body text

How to Pause Program Execution in C using Sleep Functions?

Linda Hamilton
Release: 2024-10-24 03:31:30
Original
834 people have browsed it

How to Pause Program Execution in C   using Sleep Functions?

Seeking a Sleep Function in C

In pursuit of controlling program execution flow, you may encounter the need to pause the program for a specified duration. In C , there's no direct equivalent to the commonly used Sleep(time) function found in other languages. However, the C standard library provides a viable alternative.

Introducing std::this_thread::sleep_for

The C Standard Library offers the std::this_thread::sleep_for function, which allows you to suspend the current thread's execution for a specified duration. To use it, you'll need to include the following headers:

<code class="cpp">#include <chrono>
#include <thread></code>
Copy after login

The syntax of std::this_thread::sleep_for is as follows:

<code class="cpp">void sleep_for(const std::chrono::duration& timespan);</code>
Copy after login

Where timespan is a std::chrono::duration object specifying the duration of the sleep. To create a millisecond-based std::chrono::duration, use the std::chrono::milliseconds constructor:

<code class="cpp">std::chrono::milliseconds timespan(111605);</code>
Copy after login

Using these components, you can halt the execution of your program for the desired time interval:

<code class="cpp">std::this_thread::sleep_for(timespan);</code>
Copy after login

Note that std::this_thread::sleep_until serves as a complementary function for precise time-based synchronization.

Legacy Sleep Options (Pre-C 11)

Prior to the introduction of C 11, the language lacked thread functionality and sleep capabilities. Consequently, the solution to suspend execution was platform-dependent. For Windows or Unix, you might have relied on something like this:

<code class="cpp">#ifdef _WIN32
    #include <windows.h>
    
    void sleep(unsigned milliseconds)
    {
        Sleep(milliseconds);
    }
#else
    #include <unistd.h>
    
    void sleep(unsigned milliseconds)
    {
        usleep(milliseconds * 1000); // takes microseconds
    }
#endif</code>
Copy after login

Alternatively, you could have simplified the implementation using the Boost library's boost::this_thread::sleep.

The above is the detailed content of How to Pause Program Execution in C using Sleep Functions?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!