简体   繁体   English

Task Parallel Library如何执行负载平衡?

[英]How Task Parallel Library performs load balancing?

We all know that in order to really perform asynchronous operations, our machine have to have multiple cores that each one of them will run its' own thread that will execute its' task. 我们都知道,为了真正执行异步操作,我们的机器必须具有多个内核,每个内核将运行自己的线程,该线程将执行其任务。

Lets start with an example in which we have a quad core cpu and 4 tasks . 让我们从一个例子开始,其中有一个四核cpu4个任务 In order to run our four tasks in real parallelism using C# we will have to create and run 4 tasks separately like so: 为了使用C#以真正的并行度运行我们的四个任务,我们将必须分别创建和运行四个任务,如下所示:

public static void Main()
{    
   // Define and run the tasks.
   Task[] tasks = {
      Task.Run( () => WorkA() ),
      Task.Run( () => WorkB() ),
      Task.Run( () => WorkC() ),
      Task.Run( () => WorkD() )
   }

   Task.WaitAll(tasks);
}

In this example TPL will supply each task with a thread from the ThreadPool and run each thread in a different processor if possible. 在此示例中, TPL将为每个任务提供来自ThreadPool的线程,并在可能的情况下在不同的处理器中运行每个线程。 (I hope I am right) (我希望我是对的)

The scenario: 场景:

Lets say that we have quad core cpu and 6 tasks and we wrote the following code: 假设我们有四核cpu6个任务 ,我们编写了以下代码:

public static void Main()
{    
   // Define and run the tasks.
   Task[] tasks = {
      Task.Run( () => WorkA() ),
      Task.Run( () => WorkB() ),
      Task.Run( () => WorkC() ),
      Task.Run( () => WorkD() ),
      Task.Run( () => WorkE() ),
      Task.Run( () => WorkF() )
   }

   Task.WaitAll(tasks);
}

Will TPL know what is the best sufficient amount of threads and schedule the tasks among them? TPL会知道什么是最好的足够数量的线程并计划其中的任务吗? (considering the cores' states and the tasks' load differences) (考虑核心的状态和任务的负载差异)

for example: 例如:

  • Total of 3 threads - 2 tasks per thread. 总共3个线程-每个线程2个任务。 (Lets say one core is busy) (可以说一个核心很忙)
  • Total of 4 threads - 3 threads - 1 task per thread and 3 tasks for the last one. 总共4个线程-3个线程-每个线程1个任务,最后一个任务3个任务。
  • Total of 6 threads - 1 task per thread. 总共6个线程-每个线程1个任务。

How does TPL perform load balancing? TPL如何执行负载平衡? What TPL takes in consideration in order to provide the best load balancing.. TPL考虑什么以提供最佳的负载平衡。

Will TPL know what is the best sufficient amount of threads and schedule the tasks among them? TPL会知道什么是最好的足够数量的线程并计划其中的任务吗?

No. It's more complicated then you think: 否。然后您会觉得更复杂:

Task.Run() adds a Task to the global queue. Task.Run()将Task添加到全局队列。 Suppose you already have 4 idle threads in your pool, then they will try to take Tasks from the global queue, an operation that involves synchronization and locks. 假设您的池中已经有4个空闲线程,那么它们将尝试从全局队列中获取Tasks,该操作涉及同步和锁定。 Depends on the workload of each Task, it could be more efficient to let just one or two threads to handle all the tasks. 根据每个任务的工作量,让一个或两个线程来处理所有任务可能会更有效率。 And what if there is only one thread in the pool? 如果池中只有一个线程怎么办? Creating 3 more threads might be more expensive then the execution of the Tasks by the single thread. 创建3个以上的线程可能比通过单个线程执行任务更昂贵。 That information is not available during runtime, of course. 当然,该信息在运行时不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM