【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的核心数多时会降低系统的性能。

相关推荐
PAK向日葵1 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱装代码的小瓶子2 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
Maybe_ch4 小时前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨4 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
终焉暴龙王4 小时前
CTFHub web进阶 php Bypass disable_function通关攻略
开发语言·安全·web安全·php
Edingbrugh.南空5 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
CodeCraft Studio5 小时前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
QQ_4376643145 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae5 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
封奚泽优6 小时前
使用Python实现单词记忆软件
开发语言·python·random·qpushbutton·qtwidgets·qtcore·qtgui