为了好玩,我创建了一个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++;
}
}
}
答案 0 :(得分:7)
你的两个线程正在操纵相同的输出缓冲区,相互覆盖。如果可以避免,请不要在线程之间共享内存;所有这一切都是悲伤。
如果本练习的目的是学习如何操作原始线程,那么请退后一步,研究为什么在两个线程之间共享内存是一个坏主意。
如果本练习的目的是并行化分形的计算,那么就忘记操作原始线程。您将更好地学习如何使用任务并行库。
线程是逻辑 worker ,谁想管理一堆工人? TPL鼓励您将并行化视为可以并行完成的任务的操作。让TPL负责确定要分配给您的任务的工人数量。
答案 1 :(得分:1)
代码中的问题是多次初始化this.output
(每个线程一次)。
两个线程都使用相同的this
,当第一个线程初始化this.output
的列时,第二个线程重新初始化它,第一个线程丢失其分配的内存。
因此,this.output[x]
将不再存在于第一个帖子中(missing an object reference
例外)。
这也解释了为什么你的代码只用一个线程运行完美。
简单的解决方案是在一开始就初始化整个数组。
答案 2 :(得分:0)
你的逻辑似乎很可疑。
但是如果你认为它是正确的并且在每个线程中需要重置this.output
数据,那么请执行以下操作:
[x][y]
更改为[x,y] [][]
更改为[,]
lock
this.output
醇>