c++微处理器的线程划分

发布于 2024-10-02 20:43:40 字数 154 浏览 0 评论 0原文

我有一个问题...我需要构建一个多线程应用程序,我的问题是:如果我有一个 2cpu 处理器,我的 2 个线程是否会自动按处理器分开? 如果我有 4 个线程并且我的电脑有 4 个 CPU,那么每个处理器又是 1 个吗?如果我有 4 个处理器和 2 个 cpu,如何划分?

提前致谢

I have a question... I need to build an app multi-thread and my question is: if I have a 2cpu processor, is automatically that my 2 threads are separately one by processor?
and if I have 4 threads and my pc have 4cpu, are again 1 per processor? and if I have 4 processor and 2 cpus, how is divided??

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅冕 2024-10-09 20:43:40

这实际上不是一个可以回答的问题,除非您至少指定操作系统。

C++ 本身对线程一无所知,它们是操作系统向执行环境提供的服务,并且依赖于该操作系统来实现。

作为一般观察,我非常确定 Linux 独立调度线程,以便多个线程可以分布在不同的 CPU 和/或内核上。我怀疑 Windows 也会做同样的事情。

某些操作系统将允许您指定线程亲和性,即线程(有时是线程组)坚持使用单个 CPU 的能力,但同样,这是操作系统问题而不是 C++ 问题。

对于Windows(根据您的评论),您可能需要阅读此介绍。 Windows 提供了 SetProcessAffinityMask() 函数来控制给定进程中所有线程的关联性,或者提供 SetThreadAffinityMask() 来独立控制线程。

但是,通常,您会发现最好不要管这些,让操作系统来解决 - 除非您对不同的行为有特定的需求,否则操作系统几乎肯定会做出正确的决定。

This is not really a question which can be answered unless you specify the operating system at a minimum.

C++ itself knows nothing of threads, they are a service provided by the OS to the execution environment, and depend on that OS for its implementation.

As a general observation, I'm pretty certain that Linux schedules threads independently so that multiple threads can be spread across different CPUs and/or cores. I suspect Windows would do the same.

Some OS' will allow you to specify thread affinity, the ability for threads (and sometimes groups of threads) to stick with a single CPU but, again, that's an OS issue rather than a C++ one.

For Windows (as per your comment), you may want to read this introduction. Windows provides a SetProcessAffinityMask() function for controlling affinity of all threads in a given process or SetThreadAffinityMask() for controlling threads independently.

But, usually, you'll find it's best to leave these alone and let the OS sort it out - unless you have a specific need for different behaviour, the OS will almost certainly make the right decisions.

烟若柳尘 2024-10-09 20:43:40

线程如何分配给处理器取决于您的应用程序运行的操作系统。通常,大多数操作系统不会对如何在处理器之间分割线程做出任何保证,尽管有些操作系统确实有一些低级 API 允许您指定线程关联性。

How threads get allocated to processors is specific to the OS your application is running on. Typically most OS's don't make any guarantees about how your threads are split across the processors, although some do have some low level APIs to allow you to specify thread affinity.

淑女气质 2024-10-09 20:43:40

如果您的线程受 CPU 限制,那么它们肯定会被调度到所有可用的 CPU 上。

如果您的线程受 IO 限制,那么如果每个 CPU 只有一个线程,则大多数 CPU 将处于空闲状态。这就是为什么 - 当尝试最大化性能时 - 衡量正在发生的情况并找到每个 CPU 的硬编码线程比率,或者使用操作系统线程池机制非常重要,该机制可以访问足够的信息来保留完全相同的线程数由于有 CPU 核心而处于活动状态。

您通常不希望拥有比 CPU 更多的活动线程(即不会被阻塞等待 IO 完成的线程),因为在 CPU 上的活动线程之间切换的行为确实会产生很小的成本,而且这些成本可能会增加。

If your threads are CPU bound, then they will certainly tend to be scheduled on all available CPUs.

If your threads are IO bound, then if you only have one thread per CPU, most of the CPUs will be sitting idle. This is why - when attempting to maximize performace - it is important to measure what is happening and either find a hard coded ratio of threads per CPU, or use the operating systems thread pooling mechanism which has access to enough information to keep exactly as many threads active as there are CPU cores.

You generally dont want to have MORE active threads that CPUs (i.e. threads that arn't blocked waiting for IO to complete) as the act of switching between active threads on a CPU does incur small cost that can add up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文