只是为了好玩,我创建了一个 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++;
}
}
}