并行和分工在C#?

2023-09-03 07:27:31 作者:◇ǒの拜辞

(假设我有10个内核)的

在我写的:

 的Parallel.For(0,100,(我,州)=>
         {
            Console.WriteLine(ⅰ);
         });
 

问题:

什么是分配的数字量每个核心的公式? (它是 100/10 ?)

C 教程之并发编程的几个误解

目前执行点,它每个核心的已经知道这号码是是要去处理?抑或是每次使用来自 [0..100] 库一个新的数字/秒(让我们忽略块或区域现在)?

参数 - 它指的是 0..100 指数或者是一个每个线程和它的会处理的数字相对指数?

解决方案

这是没有记录。也许是工作人员工作检查出的工作块从共享工作池。也许他们只需要不到10个项目的一次,因为采取10项将导致少于10个线程是在工作附近的的Parallel.For 操作的结束。单个慢线程可以创建一个连续的瓶颈,因为它可能是唯一的线程仍在运行。这是更好地使这一类型的瓶颈变小分区的工作在较小的块。

我不知道。我是pretty的肯定人会使用反射来撬开这些信息了。

它指的是全局索引,所以你可以用它来访问共享目录或什么的。每个见过你的工作代表将是独一无二的。

(let's assume I have 10 cores)

When I write:

Parallel.For(0, 100, (i,state) =>  
         { 
            Console.WriteLine(i); 
         });

Questions:

What is the formula of assigning amount of numbers to each core? (is it 100/10?)

At execution point, does each core already know which numbers is is gonna handle? Or does it consume each time a new number/s from the [0..100] repository (let's ignore chunk or range for now)?

The i parameter - does it refer to the 0..100 index or is it a relative index in each thread and its "gonna handle" numbers?

解决方案

It is not documented. Probably the worker tasks check out chunks of work from a shared work pool. Probably they take less than 10 items at once because taking 10 items would lead to less than 10 threads being at work near the end of the Parallel.For operation. A single slow thread could create a sequential bottleneck because it might be the only thread still running. It is better to partition the work in smaller chunks so that this type of bottleneck becomes smaller.

I don't know. I'm pretty sure one would have to pry this information out using Reflector.

It refers to the global index so you can use it to access a shared list or something. Every i ever seen by your work delegate will be unique.

相关推荐