C++什么数据类型是thread_id,它能分配给变量吗
C++ What datatype is a thread_id and can it assigned to a variable?
我很好奇线程的数据类型是什么,以及它是否可以分配给变量,这样做是否明智/有用。
使用#include <thread>
库。
std::thread
是表示单个执行线程的类。
它本身并不是一个操作系统线程。它只是代表它。
您可以创建线程类型的对象:
thread t1(foo);
你可以移动构造这样的对象,也可以移动分配这样的对象:
thread t2,t3;
t3=thread(foo); // move assignement t3 start function foo() now
t2=move(t3); // t2 takes over what t3 was representing
但你不能复制线程:
//t2=t3; // not possible to copy threads; you have to move them
线程的标识可以通过thread::id
类型(依赖于实现的类型)的值来完成。然而,native_handle()
也返回了一个thread::native_handle_type
,它可以(如果实现支持的话)返回一个可以用于操作系统特定功能的标识符
在线演示
相关文章:
- 何时在函数中声明堆栈分配变量?
- 取消引用指针并立即为其分配变量,导致分段错误
- 在C++中动态分配变量类型
- 在函数 (c++) 中分配变量类型和大小数组
- 无法在命名空间范围内分配变量
- 自我分配变量的定义
- 在 CUDA C++中分配变量 - 无法分配设备变量
- 在 CPP 中动态分配变量的数据类型
- 为什么无法在类主体中分配变量?
- const 类方法是否阻止在类外部分配变量?
- 从文件中分配变量,并使用定界符分配
- 分配指针后,是否可以为指针指向的事物分配变量名称?
- 读取外部文件并在类似xml的输入中分配变量
- 有没有办法在不分配变量的情况下将字符串连接到数字
- 如果使用alloca在内联函数中的堆栈上分配变量,那么在内联函数返回后,其引用是否有效
- 堆分配变量的结构成员对齐
- 包含溢出缓冲区的堆栈分配变量,也称为缓冲区
- 在 GetTransfer 之后,我在帐户 1 收到未分配变量错误。我无法弄清楚我搞砸了什么逻辑错误
- 对动态分配的变量的引用与指向相同动态分配变量的指针
- 在C++中为用户输入分配变量