Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/sleep for"

From cppreference.com
< cpp‎ | thread
(simplify by macro)
m ({{c}}, headers sorted)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{cpp/title|n=this_thread::|sleep_for}}
 
{{cpp/title|n=this_thread::|sleep_for}}
 
{{cpp/thread/navbar}}
 
{{cpp/thread/navbar}}
{{ddcl | header=thread | since=c++11 | 1=
+
{{ddcl|header=thread|since=c++11|1=
 
template< class Rep, class Period >
 
template< class Rep, class Period >
 
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
 
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
 
}}
 
}}
  
Blocks the execution of the current thread for ''at least'' the specified {{tt|sleep_duration}}.
+
Blocks the execution of the current thread for ''at least'' the specified {{c|sleep_duration}}.
  
This function may block for longer than {{tt|sleep_duration}} due to scheduling or resource contention delays.  
+
This function may block for longer than {{c|sleep_duration}} due to scheduling or resource contention delays.
  
 
The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
 
The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.
Line 14: Line 14:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | sleep_duration | time duration to sleep}}
+
{{par|sleep_duration|time duration to sleep}}
 
{{par end}}
 
{{par end}}
  
Line 21: Line 21:
  
 
===Exceptions===
 
===Exceptions===
Any exception thrown by clock, time_point, or duration during the execution (clocks, time points, and durations provided by the standard library never throw).
+
Any exception thrown by {{tt|clock}}, {{tt|time_point}}, or {{tt|duration}} during the execution (clocks, time points, and durations provided by the standard library never throw).
  
 
===Example===
 
===Example===
{{example|
+
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <chrono>
 
#include <chrono>
 +
#include <iostream>
 
#include <thread>
 
#include <thread>
 
   
 
   
 
int main()
 
int main()
 
{
 
{
#if __cplusplus >= 201402L
+
     using namespace std::chrono_literals;
     using namespace std::literals;
+
 
#endif
+
     std::cout << "Hello waiter\n" << std::flush;
     std::cout << "Hello waiter" << std::endl;
+
 
     auto start = std::chrono::high_resolution_clock::now();
+
     const auto start = std::chrono::high_resolution_clock::now();
#if __cplusplus >= 201402L
+
     std::this_thread::sleep_for(2000ms);
     std::this_thread::sleep_for(2s);
+
     const auto end = std::chrono::high_resolution_clock::now();
#else
+
     const std::chrono::duration<double, std::milli> elapsed = end - start;
    std::this_thread::sleep_for(std::chrono::seconds(2));
+
 
#endif
+
     std::cout << "Waited " << elapsed << '\n';
     auto end = std::chrono::high_resolution_clock::now();
+
     std::chrono::duration<double, std::milli> elapsed = end-start;
+
     std::cout << "Waited " << elapsed.count() << " ms\n";
+
 
}
 
}
|p=true
+
|p=true
| output=
+
|output=
 
Hello waiter
 
Hello waiter
Waited 2000.12 ms
+
Waited 2000.13 ms
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/thread/dsc sleep_until}}
+
{{dsc inc|cpp/thread/dsc sleep_until}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 10:26, 23 October 2023

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
sleep_for
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
Defined in header <thread>
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
(since C++11)

Blocks the execution of the current thread for at least the specified sleep_duration.

This function may block for longer than sleep_duration due to scheduling or resource contention delays.

The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.

Contents

[edit] Parameters

sleep_duration - time duration to sleep

[edit] Return value

(none)

[edit] Exceptions

Any exception thrown by clock, time_point, or duration during the execution (clocks, time points, and durations provided by the standard library never throw).

[edit] Example

#include <chrono>
#include <iostream>
#include <thread>
 
int main()
{
    using namespace std::chrono_literals;
 
    std::cout << "Hello waiter\n" << std::flush;
 
    const auto start = std::chrono::high_resolution_clock::now();
    std::this_thread::sleep_for(2000ms);
    const auto end = std::chrono::high_resolution_clock::now();
    const std::chrono::duration<double, std::milli> elapsed = end - start;
 
    std::cout << "Waited " << elapsed << '\n';
}

Possible output:

Hello waiter
Waited 2000.13 ms

[edit] See also

stops the execution of the current thread until a specified time point
(function) [edit]