【C++】std::thread的英文资料整理

2023年9月10日,周日上午

这篇博客是我对网上的英文资料的整理和翻译


英文资料来自:

c++ - What exactly is an std::thread? - Stack Overflow


When you create a std::thread (assuming you don't use the default constructor), the constructor will use the operating system API to create a native thread, and calls the passed function.

当你创建一个std::thread类的对象,这个类的构造函数会使用操作系统的API去创建一个原生的线程,并调用你传递进来的函数。

std::thread represents a thread, managed by the operating system. It has its stack, registers, instruction pointer etc. However, it is still managed by the OS. The operating system handles all the scheduling, assigning the thread to the hardware core and then preempting it if necessary to do another work on that core.

std::thread类代表一个被操作系统管理的线程。这个线程有自己的栈、寄存器、指令指针等等。操作系统掌握所有的线程调度,能够把线程分配给CPU的核心,还可以在其他任务需要CPU的核心时停止线程。

If you launch more threads than there are cores on your CPU, and they all run all the time, the OS will start swapping them in and out, effectively keeping them all running. However, this swapping is not for free, and you can slow everything down if you have too many of them.

如果你启动的线程数比CPU的核心数要多,并且这些线程会一直运行,那么操作系统会开始交替执行这些线程,以确保这些线程能有效地"同时"运行。但是,交替运行是有代价的,如果你有太多的线程,那么你会让一切事情变慢。

However, if your threads are halted for whatever reason -- for example, you threads stop on a mutex, wait on a condition variable, or simply goes to sleep (e.g. via std::this_thread::sleep_for), then it no longer consume the hardware resources during that wait.

如果你的线程因为某些原因休眠了,那么这个线程在休眠期间不会再消耗任何硬件资源。

The operating system has a subsystem called "process scheduler" which allocates the time of the hardware CPU cores (or logical core in case of hyper threading, which I assume is what you mean by "hardware thread") time for each of the threads running on the system. The number of (logical) cores the CPUs of the system has affects how many threads can be executed in parallel, but doesn't limit how many threads the operating system can manage.

As such, nothing in particular will happen or will stop happening. If your system has more threads ready to run than number of (logical) CPU cores, then the operating system will not be able to give CPU time to all of the threads in parallel.

Note that creating native threads has a performance penalty, and having more threads waiting to run (excluding those waiting for disk or network) than the number of cores to execute them will reduce the performance of the system.

操作系统有一个负责进程调度的子系统,这个子系统会分配CPU的核心的时间给每一个运行在操作系统上的线程。CPU的核心数决定能有多少线程可以并行,但不会限制能有多少线程并发。

即使准备运行的线程数比CPU的核心数要多,也不会有什么事情发生或停止运行,只是操作系统会没有足够的CPU时间来进行并发。

需要注意的是创建原生线程有性能上的损耗。因为等着被执行的线程要比CPU的核心数多时会降低系统的性能。

相关推荐
寻星探路7 分钟前
【Python 全栈测开之路】Python 进阶:库的使用与第三方生态(标准库+Pip+实战)
java·开发语言·c++·python·ai·c#·pip
SmartRadio8 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
rfidunion8 小时前
QT5.7.0编译移植
开发语言·qt
rit84324998 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
微露清风9 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习
大、男人9 小时前
python之asynccontextmanager学习
开发语言·python·学习
hqwest9 小时前
码上通QT实战08--导航按钮切换界面
开发语言·qt·slot·信号与槽·connect·signals·emit
CSARImage9 小时前
C++读取exe程序标准输出
c++
一只小bit9 小时前
Qt 常用控件详解:按钮类 / 显示类 / 输入类属性、信号与实战示例
前端·c++·qt·gui
AC赳赳老秦9 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek