user1158775
user1158775

Reputation: 43

Concurrently write to an object in C#

I am creating an image processing application in C# and trying to take advantage of my multicored system by performing the bulk of the processing in parallel. Specifically, I have a Parallel.For loop that walks through each pixel and applies a Gaussian filter. Just performing the calculations, I see a significant speed up, as expected, but the problem comes when trying to save the results of the computations. The code looks something like this:

Parallel.For(// various loop parameters
{
    // processing and number crunching
    // significant speed up here
    .
    .
    .
    bitmap.SetPixel(x, y, value); 
});

This gives a runtime error because multiple threads end up trying to write to the bitmap object at the same time. I locked the object as follows:

lock(bitmap)
    bitmap.SetPixel(x, y, value); 

But this ends up being slower than the serial version. After examining it, it turns out the call to SetPixel takes about 90% of the run time for each loop iteration, meaning that the separate threads were spending the bulk of their time waiting to acquire the lock on the bitmap object, and the added overhead of having multiple threads slowed it down.

So what I want to know is, is there a way to have multiple threads concurrently write to the same object? I am careful that each thread is working on a different part of the image, so no race condition would be introduced by this. Is there any way to override the error that's being thrown and say, "I know what I'm doing is unsafe, but I want to do it anyways?" Something like the Concurrency::combinable class seems like it might do the trick, but that's only in C++.

Upvotes: 4

Views: 429

Answers (1)

Lee
Lee

Reputation: 144136

If you are filtering in the whole image you should consider locking the bitmap, copying the image data to an array and then concurrently updating the array data instead of calling SetPixel.

Upvotes: 4

Related Questions