多核用法,线程,线程池 [英] Multi-core usage, threads, thread-pools

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

问题描述

我对多线程编程和多核用法有一些疑问.

I have some questions about multi-threaded programming and multi-core usage.

我特别想知道操作系统和/或框架(这是.NET)如何处理频繁使用的内核.

In particular I'm wondering how the operating system and/or framework (this is .NET) deals with cores that are heavily used.

这是我关于线程的问题:

Here's my questions regarding threads:

  • 产生新线程时,将线程分配给特定核心的算法是什么?
  • When a new thread is spawned, what is the algorithm for assigning the thread to a particular core?
  1. 循环法算法
  2. 随机
  3. 当前使用最少的内核

  • 如果不是当前使用最少的内核,那么确定这种类型的代码是否会使线程的典型用法相形见and,从而使事情变得更糟?
  • 线程在生命周期中是否从一个核心移动到另一个核心?如果是这样,这是否可以处理由于某种原因而被过度使用"的内核,从而操作系统尝试将线程改组为使用较少的内核以帮助系统?如果不是,又为什么呢?
  • 我的最后一个问题,基本上是上述内容的重用,是关于.NET ThreadPool类的,该类处理.BeginInvoke之类的东西.这个课程有没有做这些东西?如果没有,为什么不应该呢?

    My final question, which is basically a reuse of the above, is about the .NET ThreadPool class, which handles things like .BeginInvoke and such. Does this class do any of this stuff? If not, why not, or should it?

    是否有任何方法可以调整此处理,在操作系统中暗示该特定线程,请在为其分配内核时多加注意,因为我知道它将使用大量cpu.那有道理吗?还是很多cpu"只是相对的,因此还不够好?

    Is there any way to tweak this handling, sort of hint at the operating system that this particular thread, please pay a bit more attention to it when you assign it a core, since I know it will use a lot of cpu. Would that make sense? Or would "a lot of cpu" just be relative and thus not really good enough?

    推荐答案

    产生新线程时,什么是 分配线程的算法 到特定的核心?

    When a new thread is spawned, what is the algorithm for assigning the thread to a particular core?

    这完全取决于操作系统.答案通常是经过大量修改的轮询方案.每x毫秒,一个内核就会被中断,并在其上放置一个新线程(因此,没有使用最少的内核".只要有线程可以运行,每个内核都会有事要做).

    That depends entirely on the OS. And the answer is usually a heavily modified round-robin scheme. Every x milliseconds, a core is interrupted, and a new thread placed on it (so there is no "least used core". As long as there are threads ready to run, every core will have something to do).

    在Windows中,我相信总是 选择执行优先级最高的线程/进程. (因此,如果您有一个进程在单核系统上具有较高的优先级运行,则该进程将有可能100%的时间运行,从而使所有其他进程都饿死.当然,这仅在该进程永不阻塞的情况下才适用,在这种情况下这不太可能现实世界.

    In Windows, I believe the highest-prioritized thread/process is always selected for execution. (So if you have one process running at high priority on a single-core system, that process will potentially run 100% of the time, starving out every other process. Of course this only applies if that process never blocks, which is unlikely in the real world.

    当然,由于现代操作系统(例如Windows)很复杂,因此还有很多其他功能.有时会对某些进程给予优先处理,但是根据经验,Windows将始终选择高优先级的进程(这就是为什么您可以通过在单核时代将进程实时"赋予优先级来冻结计算机的原因)

    And of course, because a modern OS such as Windows is complex, there's a lot more to it. Certain processes are given preferential treatment from time to time, but as a rule of thumb, Windows will always pick high-priority processes (which is why you could pretty much freeze your computer by giving a process "realtime" priority back in the singlecore days)

    在Linux下,优先级较低的进程也会定期调度,而不是经常调度.

    Under Linux, lower-prioritized processes are regularly scheduled as well, just not as often.

    但是您能做的最好的事情通常是假设操作系统将制定出合理的方案,然后尝试与系统的其余部分配合使用. (当您无事可做时,将处于ield/block/sleep状态,从而允许其他线程运行).

    But the best you can do is typically just assume that the OS will work out a fair scheme, and then try to play nice with the rest of the system. (Yield/block/sleep when you have nothing to do, allowing other threads to run).

    线程是否从一个核心移到了 他们一生中的另一个?

    Are threads moved from one core to another during their lifetime?

    当然.假设您在双核系统上运行了三个线程.给我看一个公平的时间表,它不涉及在内核之间定期移动线程.

    Of course. Imagine you have three threads running on a dualcore system. Show me a fair schedule that doesn't involve regularly moving threads between cores.

    我的最后一个问题,基本上是 上面的重用是关于 .NET ThreadPool类,用于处理 诸如.BeginInvoke之类的东西. 这个课程有没有做这些东西? 如果不是,为什么不呢? 什么东西线程调度和选择要运行的内核?不,线程池只是一种将线程重用于多个任务的机制,而不必为每个任务创建一个新线程,然后再将其关闭.

    My final question, which is basically a reuse of the above, is about the .NET ThreadPool class, which handles things like .BeginInvoke and such. Does this class do any of this stuff? If not, why not, or should it? What stuff? Thread scheduling and choosing cores to run on? No, the threadpool is simply a mechanism to reuse threads for muliple tasks, instead of having to create a new thread for every single task, and then closing it afterwards.

    是否有任何方法可以对此进行调整 处理,有点暗示 那个特殊的操作系统 线程

    Is there any way to tweak this handling, sort of hint at the operating system that this particular thread

    这就是线程/进程优先级的目的.如果您的线程必须占用大量CPU时间,即使有其他占用大量CPU的线程正在运行,请提高该线程的优先级. 但是要小心.通常,没有多少CPU密集型线程在运行,这意味着即使以普通优先级运行,您也将获得99.9%的CPU时间.就像我说的那样,Windows会非常积极地调度优先级更高的线程,因此只有在您真的表示认真时才提高优先级.

    That's what thread/process priority is for. If you have a thread that must get lots of CPU time, even when there are other CPU-intensive threads running, raise the thread's priority. But handle with care. Usually, there aren't many CPU-intensive threads running, which means that even at normal priority, you'll get 99.9% of the CPU time. As I said, Windows schedules higher-prioritized threads very aggressively, so only raise priority if you really mean it.

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

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