C# 线程简单计算

C# 线程简单计算,c#,.net,multithreading,parallel-processing,C#,.net,Multithreading,Parallel Processing,我一直在尝试将下面的函数并行化,但不知道如何实现 public static Cell GetClosestCell (Cell cell) { // The four calls below should be run in parallel. Cell temp1 = new FindNorth(cell); Cell temp2 = new FindSouth(cell); Cell temp3 = new FindWest(cell); Cell

我一直在尝试将下面的函数并行化,但不知道如何实现

public static Cell GetClosestCell (Cell cell)
{
    // The four calls below should be run in parallel.
    Cell temp1 = new FindNorth(cell);
    Cell temp2 = new FindSouth(cell);
    Cell temp3 = new FindWest(cell);
    Cell temp4 = new FindEast(cell);

    // Return smallest cell based on [X].
    if ((temp1.X < temp2.X) && (temp1.X < temp3.X) && (temp1.X < temp4.X))
    {
        return (temp1);
    }
    else if ((temp2.X < temp3.X) && (temp2.X < temp4.X))
    {
        return (temp2);
    }
    else if (temp3.X < temp4.X)
    {
        return (temp3);
    }
    else
    {
        return (temp4);
    }
}
公共静态单元格GetClosestCell(单元格)
{
//下面的四个调用应该并行运行。
单元temp1=新的FindNorth(单元);
Cell temp2=新的FindSouth(Cell);
单元格temp3=新查找的西部(单元格);
单元格temp4=新的FindEast(单元格);
//返回基于[X]的最小单元格。
如果((temp1.X
四个函数调用中的每一个都应该并行运行,但不必启动线程。换句话说,应该有4个线程已经在运行,等待我可以分派每个调用的输入

我已经习惯了并行循环的正常模式,不知道如何实现这一点(至少不是以一种干净的方式); 使用System.Threading.Tasks; 公共课程{ 公共静态void Main(){ 任务task1=新任务(n=>FindNorth((单元格)n),单元格); tasktask2=新任务(n=>FindSouth((单元格)n),单元格); tasktask3=新任务(n=>FindSouth((Cell)n),Cell); 任务task4=新任务(n=>FindEast((单元格)n),单元格); task1.Start(); task2.Start(); task3.Start(); task4.Start(); Console.WriteLine(“主方法完成。按以完成”); Console.ReadLine(); } }
+1,但我会使用.NET 4.0中的
Task.Factory.StartNew()
和.NET 4.5中的
Task.Run()
在单个操作中创建和启动
任务。我还将在末尾添加
Task.WaitAll()
。一个闭包就足以传递
单元格
参数,无需显式传递。谢谢。这不是每次代码运行时都会创建4个线程吗?我想要的是在程序启动时启动4个线程,等待处理单元。实际上,每个findxxx函数中的代码花费的时间很短,因此每次调用时启动一个新线程将无法达到线程的目的。@Raheel:不,任务的默认调度程序使用线程池,这意味着线程已经在运行;他们只接受要做的工作。还有一些开销,如果你正在做的工作量足够小,那么把工作分成这么小的部分就不值得了。相反,如果要执行一系列
GetClosestCell
操作,请使用
Parallel.ForEach(cells,cell=>GetClosestCell(cell))
@AllonGuralnek:谢谢。不过,这就是问题所在。GetClosestCell本身有一个循环依赖项,因此只能使用上一次调用的输出调用它。我会试试你的建议,看看是否真的有什么不同。你说FindXxx花的时间很少。如果是这样的话,就没有办法将这4个步骤并行化:开销将始终主导有用的工作。在更高的层次上并行化,我做不到。高于此函数的任何内容都是连续的,并且取决于上一次迭代。有没有办法让4个线程保持运行,并在队列中的数据可用时立即轮询到进程中?当然有,但从该队列中获取数据通常会导致等待下一项(这通常需要旋转或操作系统同步(昂贵))。FindXxx方法的速度有多快(每个核心每秒调用多少次)?如果他们太快,你也无能为力。;极端示例:如何并行化两个整数加法?不可能。我会说每次通话大约400到600毫秒。GetClosestCell函数本身被调用了数百万次,因此它的总调用量为0.400ms?这很容易并行化。只需使用现有答案中基于任务的解决方案。这将使用线程池。
using System;
using System.Threading.Tasks;

public class Program {
    public static void Main() {

        Task<Cell> task1 = new Task<Cell>(n => FindNorth((Cell)n), cell);

        Task<Cell> task2 = new Task<Cell>(n => FindSouth((Cell)n), cell);
        Task<Cell> task3 = new Task<Cell>(n => FindSouth((Cell)n), cell);
        Task<Cell> task4 = new Task<Cell>(n => FindEast((Cell)n), cell);



        task1.Start();
        task2.Start();
        task3.Start();
        task4.Start();
        Console.WriteLine("Main method complete. Press <enter> to finish.");
        Console.ReadLine();
    }

}