當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Sleep用法及代碼示例

C++ 在操作係統的幫助下提供了在特定時間段內延遲或不活動狀態的函數。其他 CPU 操作將正常運行,但 C++ 中的 Sleep() 函數將在線程指定的時間內休眠當前的可執行文件。根據所使用的操作係統,可以使用 2 個庫來實現:

#include<windows.h>           // for windows

#include<unistd.h>               // for linux 

睡眠可以暫停 time_period 的執行,其中 time_period 默認以秒為單位,但我們可以將其更改為微秒。

用法:

sleep( time_period );    // time_period in seconds

範圍:time_period 以秒為單位,代表睡眠時間。

返回類型:sleep函數的返回類型是一個整數,如果函數執行成功則返回值為0,否則減去返回的時間段值。

例子:

C++


// C++ Program to show how to use
// sleep function
#include <iostream>
// Library effective with Windows
#include <windows.h>
// Library effective with Linux
#include <unistd.h>
using namespace std;
// Driver code
int main()
{
  cout << "Join the Line:\n";
  cout << "Wait for 5 seconds\n";
  // sleep will schedule rest of 
  // activities after 10 seconds
  sleep(5);
  cout << "It's your time buy ticket";
}

輸出:

C++ 中類似 sleep 的函數

1. usleep():該函數與 sleep 基本相似,但隻能與 <unistd.h> 庫一起使用。

用法:

 usleep(time_period)   // time_period in microseconds

參數:它需要time_period,其中time_period默認以微秒為單位。 1 秒 = 10^6 微秒。

返回類型:整數,如果成功則返回 0,如果過程失敗則返回 (-1 或 -exp)。

例子:

C++


// C++ Program to show the 
// use of usleep function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
    cout << "Take your Position\n";
    // sleep for 10 seconds
    cout << "Wait for 5 seconds\n";
    usleep(5000000);
    cout << "Run! Run!";
    return 0;
}

輸出:

2. sleep_for():將線程安排在指定時間。它的作用就像延遲一樣,就像睡眠函數一樣。但是,由於調度活動或者可能是資源爭用延遲,線程可能花費比計劃時間更多的時間。庫使用了<線程>。

用法:

this_<thread_name>::sleep_for(chorno:: time_duration (time_period))         

參數:time_period(獲取線程的時間)

Example:

C++


// C++ Program to demonstrate 
// sleep_for()function
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
// Driver cpde
int main()
{
  cout << "Thread is running\n";
   
  // Thread delayed for 5 seconds
  this_thread::sleep_for(chrono::milliseconds(5000));
  cout << "Thread was acquired for 5 seconds\n";
  return 0;
}

輸出:

3. sleep_until():阻塞線程的執行,直到sleep_time完成。然而,即使由於調度或資源爭用延遲而達到sleep_time,它也可能比sleep_time花費更多的時間。庫使用了<線程>。

用法:

this_<thread_name>::sleep_until(awake_time) 

參數:Sleep_time(線程被阻塞執行的同一時間)

Example:

C++


// C++ Program to demonstrate
// sleep_until()
#include <chrono>
#include <iostream>
#include <thread>
// Functioning returning 
// current time
auto now() 
{
  return std::chrono::steady_clock::now(); 
}
// Function calculating sleep time 
// with 2000ms delay
auto awake_time()
{
  using std::chrono::operator"" ms;
  return now() + 2000ms;
}
// Driver code
int main()
{
  std::cout << "Starting the operation .....\n" << 
                std::flush;
   
  // Calculating current time
  const auto start{ now() };
  // using the sleep_time to delay
  // and calculating sleep time
  // using awake_time function
  std::this_thread::sleep_until(awake_time());
  // storing time for printing
  std::chrono::duration<double, std::milli> elapsed{
      now() - start
  };
  // printing waiting time
  std::cout << "Waited for : " << 
                elapsed.count() << " ms\n";
}

輸出:



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Sleep Function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。