Skip to content

常用函数

延时

让当前线程阻塞暂停指定时长(期间不占用 CPU 资源)。

cpp
#include <thread>   // 包含 sleep_for
#include <chrono>   // 包含时间单位

int main() {
    std::cout << "开始延时..." << std::endl;
    
    // 1. 延时 2 秒(seconds)
    std::this_thread::sleep_for(std::chrono::seconds(2));
    
    // 2. 延时 500 毫秒(milliseconds)
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    
    // 3. 延时 100000 微秒(100 毫秒,microseconds)
    std::this_thread::sleep_for(std::chrono::microseconds(100000));
    
    std::cout << "延时结束!" << std::endl;
    return 0;
}