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::deque用过吗?

100编程书屋_孔夫子旧书网 某日二师兄参加XXX科技公司的C++工程师开发岗位第26面: 面试官:deque用过吗? 二师兄:说实话,很少用,基本没用过。 面试官:为什么? 二师兄:因为使用它的场景很少,大部分需要性能、且需要自动扩容的时候使用vector,需要随机插入和删除的时候可以使用list。 面试官:那你知道STL中的stack是如何实现的吗? 二师兄:默认情况下,stack使

jmeter之Thread Group(线程组)

Thread Group(线程组) 1.线程组,或者可以叫用户组,进行性能测试时的用户资源池。 2.是任何一个测试计划执行的开始点。 3.上一篇提到的“控制器”和“HTTP请求”(采集器)必须在线程组内;监听器等其他组件,可以直接放在测试计划下。 线程组设置参数的意义 我们以下图为例,进行详细说明。见下图:  区域1(在取样器错误后要执行的动作) 这个区域的主要作用很明显,在线程内

Exception processing async thread queue Exception processing async thread queue

错误信息描述: eclipse 在debug中弹出异常信息框 Exception processing async thread queue Exception processing async thread queue 解决方法: eclipse 中有一个Expressions窗口,里面添加的 expression 没有正确执行,并且没有删除,只要 Remove All Expressio

no thread-bound request found:are you referring to request

问题描述: 通过webservice接口调用程序时,发现在执行查询的时候一直报一个错误,错误信息如下: java.lang.IllegalStateExceptino:No thread-bound request found:are you referring to request attributes outside of an actual web request,or processi

FFplay源码分析-video_thread

《FFmpeg原理》的社群来了,想加入社群的朋友请购买 VIP 版,VIP 版有更高级的内容与答疑服务。 本系列 以 ffmpeg4.2 源码为准,下载地址:链接:百度网盘 提取码:g3k8 FFplay 源码分析系列以一条简单的命令开始,ffplay -i a.mp4。a.mp4下载链接:百度网盘,提取码:nl0s 。 上一篇文章已经讲解完了 audio_thread() 音频解码

FFplay源码分析-read_thread

《FFmpeg原理》的社群来了,想加入社群的朋友请购买 VIP 版,VIP 版有更高级的内容与答疑服务。 本系列 以 ffmpeg4.2 源码为准,下载地址:链接:百度网盘 提取码:g3k8 FFplay 源码分析系列以一条简单的命令开始,ffplay -i a.mp4。a.mp4下载链接:百度网盘,提取码:nl0s 。 如下图所示,本文主要讲解 read_thread() 函数的内

「生信Debug」OpenBLAS blas_thread_init: pthread_create: Resource temporarily unavailable

BLAS(Basic Linear Algebra Subprograms),翻译为基础线性代数子程序库,里面拥有大量已经编写好的关于线性代数运算的程序。OpenBLAS是其中一个实现了相关运算的开源程序库,其他软件在开发的时候就不需要额外造轮子,直接调用相关的API即可。 之前在使用OrthoFinder遇到了类似的问题,见https://github.com/davidemms/OrthoF

浅析std::ref

目录 1 为什么需要std::ref 2 std::ref使用示例 2.1 std::thread调用 2.1.1 不使用std::ref,编译失败 2.1.2 使用std::ref修饰输入变量 2.2 stl库调用(以for_each 为例) 2.3 std::bind 2.3.1 使用std::ref 2.3.2 使用placeholders::_x同样可以达到同样效果 3

C++ std::vector 的 emplace_back 能否完全取代 push_back

区别: push_back:先在调用处构造一次 class,传递进 push_back 内后再进行拷贝到缓冲区。 emplace_back:在内部直接将构造 class 的参数转发构造到缓冲区。   如果以上说法不好理解,那么用代码来表示。 // 该 Class 支持隐式构造class Class{public:Class(int a) : _a(a) {}int _a;};ve

std :: thread

头文件<thread>中包含两个类,thread和this_thread 在讲thread之前,首先要了解线程的一个状态:joinable 一个正规初始化后的线程是一个可执行线程,这种线程joinable为true,并且有一个独一无二的线程id。 相反,一个默认构造函数构造出的thread(thread(),没有参数,未初始化),joinable为false,且线程id通常和其他所有的non