std :: thread

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

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

头文件<thread>中包含两个类,thread和this_thread

在讲thread之前,首先要了解线程的一个状态:joinable

一个正规初始化后的线程是一个可执行线程,这种线程joinable为true,并且有一个独一无二的线程id。

相反,一个默认构造函数构造出的thread(thread(),没有参数,未初始化),joinable为false,且线程id通常和其他所有的non-joinable线程一样。


一个joinable线程被move from,或者调用join,detach后,变成non-joinable


1、成员类型

id 
native_handle_type 


2、成员函数

constructor
default (1)
thread() noexcept;
initialization (2)
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;

destructor如果该线程是joinable,则调用terminate()
operator=
move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;

get_idReturns the thread id.
joinablecheck if joinable
joinJoin thread
detachDetach thread
swap 
native_handleGet native handle
hardware_concurrency [static]Detect hardware concurrency

3、非成员函数(重载)

swap


4、代码示例

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::threadvoid foo() 
{// do stuff...
}void bar(int x)
{// do stuff...
}int main() 
{std::thread first (foo);     // spawn new thread that calls foo()std::thread second (bar,0);  // spawn new thread that calls bar(0)std::cout << "main, foo and bar now execute concurrently...\n";// synchronize threads:first.join();                // pauses until first finishessecond.join();               // pauses until second finishesstd::cout << "foo and bar completed.\n";return 0;
}

输出结果

main, foo and bar now execute concurrently...
foo and bar completed.


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



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

相关文章

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 :: 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