zoukankan      html  css  js  c++  java
  • c++多线程基础2(命名空间 this_thread)

    整理自:zh.cppreference.com/w/cpp/thread

    std::this_thread::yield:

    定义于头文件 <thread>

    函数原型void yield(noexcept;

    此函数的准确性为依赖于实现,特别是使用中的 OS 调度器机制和系统状态。例如,先进先出实时调度器( Linux 的 SCHED_FIFO )将悬挂当前线程并将它放到准备运行的同优先级线程的队列尾(而若无其他线程在同优先级,则 yield 无效果)

    代码:

     1 #include <iostream>
     2 #include <thread>
     3 #include <chrono>
     4 using namespace std;
     5 
     6 void little_sleep(std::chrono::milliseconds us) {
     7     auto start = std::chrono::high_resolution_clock::now();
     8     auto end = start + us;
     9     do { 
    10         std::this_thread::yield();//让出当前时间片
    11     }while(std::chrono::high_resolution_clock::now() < end);
    12 }
    13 
    14 int main(void) {
    15     auto start = std::chrono::high_resolution_clock::now();//获取当前时间
    16 
    17     little_sleep(std::chrono::milliseconds(100));
    18 
    19     auto elapsed = std::chrono::high_resolution_clock::now() - start;//计算执行 little_sleep 所用时间
    20 
    21     cout << "waited fo "
    22             << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() //将 elapsed 时间周期转化为 milliseconds 并输出
    23             << " milliseconds
    ";
    24 
    25 // 输出:
    26 // waited fo 100 milliseconds
    27 
    28     return 0;
    29 }
    View Code

    std::this_thread::get_id:

    定义于头文件 <thread>

    函数原型std::thread::id get_id(noexcept;

    得到当前线程的 id

    代码:

     1 #include <iostream>
     2 #include <thread>
     3 #include <chrono>
     4 #include <mutex>
     5 using namespace std;
     6 
     7 std::mutex g_display_mutex;
     8 
     9 void foo() {
    10     auto this_id = std::this_thread::get_id();
    11 
    12     g_display_mutex.lock();
    13     cout << "thread" << this_id << " sleeping..." << endl;
    14     g_display_mutex.unlock();
    15 
    16     std::this_thread::sleep_for(std::chrono::seconds(1));
    17 }
    18 
    19 int main(void) {
    20     std::thread t1(foo);
    21     std::thread t2(foo);
    22 
    23     t1.join();
    24     t2.join();
    25 
    26 // 输出:
    27 // thread2 sleeping...
    28 // thread3 sleeping...
    29 
    30     return 0;
    31 }
    View Code

    std::this_thread::sleep_for:

    定义于头文件 <thread>

    函数原型

    templateclass Rep, class Period >
    void sleep_forconst std::chrono::duration<Rep, Period>& sleep_duration );

    阻塞当前线程执行,以至少为指定的 sleep_duration 。

    此函数可能阻塞长于 sleep_duration ,因为调度或资源争议延迟。

    标准库建议用稳定时钟度量时长。若实现用系统时间代替,则等待时间亦可能对始终调节敏感

    异常:任何时钟、 time_point 或 duration 在执行间抛出的异常(标准库提供的时钟、时间点和时长决不抛出)

    代码:

     1 #include <iostream>
     2 #include <chrono>
     3 #include <thread>
     4 using namespace std;
     5 
     6 int main(void) {
     7     cout << "hello waiter" << endl;
     8 
     9     auto start = std::chrono::high_resolution_clock::now();
    10     std::this_thread::sleep_for(std::chrono::seconds(2));
    11     auto end = std::chrono::high_resolution_clock::now();
    12 
    13     std::chrono::duration<double, std::milli> elapsed = end - start;
    14     cout << "waited " << elapsed.count() << " ms" << endl;
    15 
    16 // 输出:
    17 // hello waiter
    18 // waited 2001.44 ms
    19 
    20     return 0;
    21 }
    View Code

    std::this_thread::sleep_until:

    定义于头文件 <thread>

    templateclass Clock, class Duration >
    void sleep_untilconst std::chrono::time_point<Clock,Duration>& sleep_time );

    阻塞当前线程,直至抵达指定的 sleep_time 。

    使用联倾向于 sleep_time 的时钟,这表示时钟调节有影响。从而在调用时间点后,阻塞的时长可能小于,但不会多于 sleep_time - Clock::now() 。函数亦可能阻塞长于抵达 sleep_time 之后,由于调度或资源争议延迟

    代码:

     1 #include <iostream>
     2 #include <chrono>
     3 #include <thread>
     4 using namespace std;
     5 
     6 int main(void) {
     7     auto start = std::chrono::high_resolution_clock::now();
     8     std::this_thread::sleep_until(start + std::chrono::seconds(2));
     9     auto end = std::chrono::high_resolution_clock::now();
    10 
    11     std::chrono::duration<double, std::milli> elapsed = end - start;
    12     cout << "waited " << elapsed.count() << " ms" << endl;
    13 
    14 // 输出:
    15 // waited 2001.42 ms
    16 
    17     return 0;
    18 }
    View Code
  • 相关阅读:
    1204整理
    JDBC(与Orcale的连接)(转)
    Listener监听器详解(转)
    Filter过滤器详解(转)
    第十二章 存货
    会计基础 第一章 总论 部分复习题目
    会计基础 第一章 总论
    星座备考之狮子座如何复习会计从业资格考试
    会计基础第八章内容2
    何以解养老之忧
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/8530239.html
Copyright © 2011-2022 走看看