std :: this_thread
来源:互联网 发布:用户画像 数据挖掘 编辑:程序博客网 时间:2024/06/14 23:30
this_thread 包装了一组可以访问当前线程信息的函数
functions
1、get_id() 获取当前线程的id。
// thread::get_id / this_thread::get_id#include <iostream> // std::cout#include <thread> // std::thread, std::thread::id, std::this_thread::get_id#include <chrono> // std::chrono::seconds std::thread::id main_thread_id = std::this_thread::get_id();void is_main_thread() { if ( main_thread_id == std::this_thread::get_id() ) std::cout << "This is the main thread.\n"; else std::cout << "This is not the main thread.\n";}int main() { is_main_thread(); std::thread th (is_main_thread); th.join();}输出结果
This is the main thread.This is not the main thread.
2、yield()
调用线程放弃执行,回到准备状态,重新分配cpu资源。所以调用该方法后,可能执行其他线程,也可能还是执行该线程
// this_thread::yield example#include <iostream> // std::cout#include <thread> // std::thread, std::this_thread::yield#include <atomic> // std::atomicstd::atomic<bool> ready (false);void count1m(int id) { while (!ready) { // wait until main() sets ready... std::this_thread::yield(); } for (volatile int i=0; i<1000000; ++i) {} std::cout << id;}int main (){ std::thread threads[10]; std::cout << "race of 10 threads that count to 1 million:\n"; for (int i=0; i<10; ++i) threads[i]=std::thread(count1m,i); ready = true; // go! for (auto& th : threads) th.join(); std::cout << '\n'; return 0;}输出结果
race of 10 threads that count to 1 million...6189370542
3、template <class Clock, class Duration>
void sleep_until (const chrono::time_point<Clock,Duration>& abs_time);
阻塞调用线程,一直到指定事件
// this_thread::sleep_for example#include <iostream> // std::cout#include <iomanip> // std::put_time#include <thread> // std::this_thread::sleep_until#include <chrono> // std::chrono::system_clock#include <ctime> // std::time_t, std::tm, std::localtime, std::mktimeint main() { using std::chrono::system_clock; std::time_t tt = system_clock::to_time_t (system_clock::now()); struct std::tm * ptm = std::localtime(&tt); std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n'; std::cout << "Waiting for the next minute to begin...\n"; ++ptm->tm_min; ptm->tm_sec=0; std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm))); std::cout << std::put_time(ptm,"%X") << " reached!\n"; return 0;}输出结果
Current time: 11:52:36Waiting for the next minute to begin...11:53:00 reached!
4、template <class Rep, class Period>
void sleep_for (const chrono::duration<Rep,Period>& rel_time);
阻塞调用线程,一直到指定时间段后。
// this_thread::sleep_for example#include <iostream> // std::cout#include <thread> // std::this_thread::sleep_for#include <chrono> // std::chrono::seconds int main() { std::cout << "countdown:\n"; for (int i=10; i>0; --i) { std::cout << i << '\n'; std::this_thread::sleep_for (std::chrono::seconds(1)); } std::cout << "Lift off!\n"; return 0;}输出结果
countdown:10987654321Lift off!
- std :: this_thread
- std::this_thread::sleep_until
- std::this_thread::yield()使用理解
- C++11 'yield' is not a member of 'std::this_thread'
- C++11 error: ‘sleep_for’ is not a member of ‘std::this_thread’
- boost::this_thread::sleep_for()死锁
- C++11 this_thread::get_id(9)
- C++11 this_thread::sleep_for(10)
- C++11 this_thread::sleep_until(11)
- C++11 this_thread::yeild(12)
- std
- std
- std
- Break the infinity thread loop using boost::this_thread::interruption_point()
- Boost编程之--慎用线程的this_thread::yield()方法
- std::map&&std::set
- std::map & std::hash_map
- std::min std::max
- delphi中的 {$R *.res}
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- java 图形界面编程(一)
- linux多线程编程
- 综合应急平台-数据库系统
- std :: this_thread
- 存储过程和函数的区别
- Git使用指南
- android gdb 调试c++代码
- JDBC call Stored Procedure
- 3幅图让你了解Spring AOP
- 如何优化关键词的质量度
- gcc 链接库的顺序问题
- 正则表达式基本语法