C#线程崩溃在第二个线程上,不确定如何修复

ray*_*b95 1 .net c# concurrency multithreading for-loop

为了好玩,我创建了一个mandelbrot程序.我现在试图通过将图像分成两个左/右部分以由两个线程处理来使其成为多线程.但是,应用程序一启动就会崩溃(尽管基于我的控制台输出,第一个线程在崩溃后继续但第二个线程永远不会启动)并且我不确定该怎么做.

崩溃是在线,this.output[x][y] = this.calculate_pixel_rgb(x, y);并说我缺少一个对象引用,我不明白,因为它适用于第一个线程.

    public void compute_all()
    {
        this.setcolors(); this.zoom_multiplier = (4 / this.zoom / this.resolution);
        Thread thread1 = new Thread(new ParameterizedThreadStart(computation_thread)); thread1.Start(new double[] { 0, 0.5 });
        Thread thread2 = new Thread(new ParameterizedThreadStart(computation_thread)); thread2.Start(new double[] { 0.5, 1 });
        thread1.Join(); thread2.Join();
    }

    public void computation_thread(object threadinfo)
    {
        double[] parameters = (double[])threadinfo;

        this.output = new int[this.resolution][][];
        for (int x = (int)(this.resolution * parameters[0]); x < (int)(this.resolution * parameters[1]); x++)
        {
            this.output[x] = new int[this.resolution][];
            for (int y = 0; y < this.resolution; y++)
            {
                this.output[x][y] = this.calculate_pixel_rgb(x, y);
                this.pixels_completed++;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Eri*_*ert 7

你的两个线程正在操纵相同的输出缓冲区,相互覆盖.如果可以避免,请不要在线程之间共享内存; 它造成的一切都是悲伤.

如果本练习的目的是学习如何操作原始线程,那么请退后一步,研究为什么跨两个线程共享内存是个坏主意.

如果本练习的目的是并行化分形的计算,那么就忘记操作原始线程.您将更好地学习如何使用任务并行库.

线程是逻辑上的工作者,谁想管理一堆工人?TPL鼓励您将并行化视为可以并行完成的任务操作.让TPL负责确定要分配给您的任务的工人数量.