使用多核 [英] Using Multiple Cores

查看:169
本文介绍了使用多核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的答案.我浏览了很多论坛帖子,这些帖子(充其量)暗示了答案可能是什么.

我的问题是,如何将执行委派给与运行主线程的内核不同的内核.我了解到,在单个内核上使用多线程,每个线程都可以快速连续地一个接一个地执行,但这不是我想要的.

在我读过的一些论坛帖子中暗示的是,在winapi中创建新线程(使用类似_beginthread()的线程)将在另一个可用的内核上自动执行该线程.我不确定,如果不是这样,那么我一直在使用的代码正在浪费其可用资源.

I have been looking all over for an answer to this question. I have looked through many forum posts that have (at best) implied what the answer could be.

My question is, how do I delegate execution to a different core than the one running the main thread. I understand that with multithreading on a single core each thread executes one after the other in quick succession, but this not what I''m looking for.

What was implied in some of the forum posts that I read is that creating a new thread in the winapi (with something like _beginthread()) will automatically execute the thread on another core if one is available. I don''t know for sure and if this isn''t the case then what code I have been using has been wasting its available resources.

推荐答案

我猜人们试图与您取得联系的是您不应该在意.多线程用于简化需要同时执行多个操作的软件设计.如果碰巧在具有多个(可用)处理器的系统上运行,则第二个好处是可以提高性能.

就像您听到的那样,(正确)由OS决定在哪个处理器上运行线程以最大化系统性能.实际上,在操作系统中,它可能会根据自己的想法随时在内核之间移动线程.

但是要回答您的问题:如果您的程序在具有2个处理器(核心)的系统上运行有2个(活动)线程,并且系统中没有其他活动线程,那么它们肯定会在不同的处理器上执行.
I guess what people were trying to get through to you is you should not care. Multiple threads are used to simplify the design of software that needs to do more than one thing simultaneously. A secondary benefit is improved performance if you happen to be running on a system with more than one (available) processor.

As you seem to have heard it is (rightly) up to the OS to decide which processor to run a thread on in order to maximise system performance. In fact in the OS may move threads between cores at its own whim.

But to answer your question: if you have a program with 2 (active) threads running on a system with 2 processors (cores), and there are no other active threads in the system then they will definitely be executed on different processors.


我同意Andrew的要求.

您将不需要担心线程执行的详细程度.该操作系统足以确保线程在等待CPU周期时不会浪费资源(如内核).
I''m in agreement with Andrew.

You will never need to worry about thread execution at this level of detail. The OS is good enough to ensure that no resources (like a core) will be wasted sitting idle, while a thread is waiting for CPU cycles.


使用CreateThread [ SetThreadAffinityMask [ ^ ]函数来设置处理器亲和力.
Threads are created using CreateThread[^], this will give you a handle to each to each thread via the return value.

By default, the operating chooses which processor to run a thread on and tries to optimise performance. However, if you want to choose for yourself, you can use the SetThreadAffinityMask[^] function to set processor affinity.


这篇关于使用多核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆