本文介绍了CPU最大线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与处理器的线程有什么关系?例如,英特尔i5具有四个内核和四个线程.

What does this have to do with the threads of the processor? for example, an Intel i5 has four cores and four threads.

我们的程序中可以使用多少个线程?例如在C ++中使用std :: thread(STL)?

How many threads can we use in our program? for example using std :: thread (STL) in C ++?

一个程序的8个线程是大线程还是少线程?

is 8 threads a big or low number of threads for a program?

推荐答案

这确实取决于.根据经验,将线程数限制为接近内核数(否则,您可能有太多上下文切换).您可以使用 std::thread::hardware_concurrency() 作为提示.通常,您使用线程池来组织程序.

It really depends. As a rule of thumb, limit the number of threads to something close to the number of cores (otherwise you might have too much context switchs). You might use std::thread::hardware_concurrency() as a hint. Often, you organize your program with a thread pool.

但是,真正重要的是 active 线程的数量.有些程序具有数百个线程,但它们的组织方式是这样的,以便在给定的瞬间它们只有少数处于活动状态(即可运行),而大多数处于空闲状态(等待IO,某些互斥量或条件变量).

However, what really matters is the number of active threads. Some programs have hundreds of threads but are organized so that only a few of them are active (i.e. runnable) at any given instant, and most of them being idle (waiting for IO, or for some mutex or condition variable).

请注意,线程是非常重的资源(特别是因为它具有自己的调用堆栈,通常至少为一个兆字节).因此,不要太多(因此,除非您有一台非常强大且扩展的计算机,否则拥有数千个线程通常是不合理的.)

Be aware that a thread is a quite heavy resource (in particular because it has its own call stack, usually at least a megabyte). So don't have too many of them (hence, having thousands of threads is generally unreasonable, unless you have a very powerful and expansive computer).

英特尔超线程技术通常令人失望.您可能不想在具有4个核心和8个超线程的Intel处理器上拥有8个活动线程.您需要进行基准测试,但是在这种情况下,您可能会预期性能会有所下降(也许拥有4个或6个活动线程会使您的整体性能更好).

The Intel hyper-threading technology is often disappointing. You probably don't want to have 8 active threads on an Intel processor with 4 cores and 8 hyperthreads. You need to benchmark, but you should expect some bad performance in such case (perhaps having 4 or 6 active threads would make your overall performance better).

线程抽象操作系统.因此,请阅读 操作系统:三件简单的书 了解有关操作系统的更多信息.

Threads are an abstraction provided and managed by the operating system. So read Operating Systems: Three Easy Pieces to understand more about OSes.

这篇关于CPU最大线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:16