std :: this_thread

2024-06-23 09:08
文章标签 std thread

本文主要是介绍std :: this_thread,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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::secondsstd::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";elsestd::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:36
Waiting 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::secondsint 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:
10
9
8
7
6
5
4
3
2
1
Lift off!




这篇关于std :: this_thread的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086723

相关文章

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

C++11的函数包装器std::function使用示例

《C++11的函数包装器std::function使用示例》C++11引入的std::function是最常用的函数包装器,它可以存储任何可调用对象并提供统一的调用接口,以下是关于函数包装器的详细讲解... 目录一、std::function 的基本用法1. 基本语法二、如何使用 std::function

Thread如何划分为Warp?

1 .Thread如何划分为Warp? https://jielahou.com/code/cuda/thread-to-warp.html  Thread Index和Thread ID之间有什么关系呢?(线程架构参考这里:CUDA C++ Programming Guide (nvidia.com)open in new window) 1维的Thread Index,其Thread

RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

目录 概述 1 RT-Thread 1.1 RT-Thread的版本  1.2 认识Nano版本 2 STM32F446U上移植RT-Thread  2.1 STM32Cube创建工程 2.2 移植RT-Thread 2.2.1 安装RT-Thread Packet  2.2.2 加载RT-Thread 2.2.3 匹配相关接口 2.2.3.1 初次编译代码  2.2.3.

GTK中创建线程函数g_thread_new和g_thread_create的区别

使用GThread函数,需要引用glib.h头文件。 这两个接口的核心区别就是  g_thread_create 是旧的接口,现在已经不使用了,而g_thread_new是新的接口,建议使用。 g_thread_create: g_thread_create has been deprecated since version 2.32 and should not be used in n

基于 rt-thread的I2C操作EEPROM(AT24C02)

一、AT24C02 The AT24C01A/02/04/08A/16A provides 1024/2048/4096/8192/16384 bits of serial electrically erasable and programmable read-only memory (EEPROM) organized as 128/256/512/1024/2048 words of 8 b

[项目][CMP][Thread Cache]详细讲解

目录 1.设计&结构2.申请内存3.释放内存4.框架 1.设计&结构 Thread Cache是哈希桶结构,每个桶是一个按桶位置映射大小的内存块对象的自由链表 每个线程都会有一个Thread Cache对象,这样每个线程在这里获取对象和释放对象时是无锁的 TLS – Thread Local Strorage Linux gcc下TLSWindows vs下TLS

线程池工具类——Thread学习笔记

记录一下线程池工具类: /*** 线程池工具类* @author lixiang* @date 2018年10月10日 - 11:10* @history 2018年10月10日 - 11:10 lixiang create.*/public class ThreadPoolHelper {private static final Logger logger = LoggerFactory.g

模拟线程死锁——Thread学习笔记

记录一下之前写过的一段模拟死锁的代码: /*** 模拟死锁** @author lixiang* @date 2018年10月12日 - 9:51* @history 2018年10月12日 - 9:51 lixiang create.*/public class HoldLockDemo {private static Object[] lock = new Object[10];priv