Race condition in ColorProcessor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Race condition in ColorProcessor

Franz Graf-2
Hi all,

I am using imageJ in JFeatureLib for all kinds of image operations.
Just recently I was informed about an IndexOutOfBoundsException in
Colorprocessor#getHistogram() L.1165: histogram[v]++;

The cause is the following sequence in a multi threaded environment:
        ImageProcessor ip = ...
        ColorProcessor.setWeightingFactors(1, 0, 0);
        ip.getHistogram();

Which is absolutely okay in single threaded environments. But in case of
multiple threads, a call to ColorProcessor.setWeightingFactors(...);
sets the rWeightd, gWeight=1d/3d, bWeight in all classes (well - static
fields)! In concurrent situations a race condition can cause a wrong
computation in L.1164:
v = (int)(r*rWeight + g*gWeight + b*bWeight + 0.5);
so that v becomes greater than the length of the array (imagine 3
concurrent calls to setWeightingFactors to 1,0,0 / 0,1,0 / 0,0,1 - in
the worst case would be an equivalent call to 1,1,1).

Currently, the only solutions I realize are:
1) synchronize on the Class to synchronize access to the static variables
        synchronize(ColorProcessor.class){
                ColorProcessor.setWeightingFactors(..)
                ip.getHistogram();
        }
  which heavily limits the parallelization
2) copy the code from ColorProcessor to use nonstatic fields/variables.
3) modify the code in ColorProcessor ... but this breaks the API
4) add an additional method (which might make look the api a bit weird)

Are there any other options that I currently do not see?
Maybe there is a recommended workflow/api already for such a case?

Thanks a lot!
Franz
--
Homepage:    http://www.Locked.de
G+: https://plus.google.com/u/0/107945158062341260943

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Race condition in ColorProcessor

Rasband, Wayne (NIH/NIMH) [E]
On Jul 14, 2013, at 4:26 PM, Franz Graf wrote:

> Hi all,
>
> I am using imageJ in JFeatureLib for all kinds of image operations.
> Just recently I was informed about an IndexOutOfBoundsException in
> Colorprocessor#getHistogram() L.1165: histogram[v]++;
>
> The cause is the following sequence in a multi threaded environment:
> ImageProcessor ip = ...
> ColorProcessor.setWeightingFactors(1, 0, 0);
> ip.getHistogram();

The ImageJ 1.48a daily build adds the non-static setRGBWeights() method that will allow you to avoid this exception by using:

   ImageProcessor ip = ...
   ((ColorProcessor)ip).setRGBWeights(1,0,0);
   ip.getHistogram();

-wayne


> Which is absolutely okay in single threaded environments. But in case of
> multiple threads, a call to ColorProcessor.setWeightingFactors(...);
> sets the rWeightd, gWeight=1d/3d, bWeight in all classes (well - static
> fields)! In concurrent situations a race condition can cause a wrong
> computation in L.1164:
> v = (int)(r*rWeight + g*gWeight + b*bWeight + 0.5);
> so that v becomes greater than the length of the array (imagine 3
> concurrent calls to setWeightingFactors to 1,0,0 / 0,1,0 / 0,0,1 - in
> the worst case would be an equivalent call to 1,1,1).
>
> Currently, the only solutions I realize are:
> 1) synchronize on the Class to synchronize access to the static variables
> synchronize(ColorProcessor.class){
> ColorProcessor.setWeightingFactors(..)
> ip.getHistogram();
> }
>  which heavily limits the parallelization
> 2) copy the code from ColorProcessor to use nonstatic fields/variables.
> 3) modify the code in ColorProcessor ... but this breaks the API
> 4) add an additional method (which might make look the api a bit weird)
>
> Are there any other options that I currently do not see?
> Maybe there is a recommended workflow/api already for such a case?
>
> Thanks a lot!
> Franz
> --
> Homepage:    http://www.Locked.de
> G+: https://plus.google.com/u/0/107945158062341260943
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Race condition in ColorProcessor

Franz Graf-2
Hi



"Rasband, Wayne (NIH/NIMH) [E]" <[hidden email]> schrieb:

>> I am using imageJ in JFeatureLib for all kinds of image operations.
>> Just recently I was informed about an IndexOutOfBoundsException in
>> Colorprocessor#getHistogram() L.1165: histogram[v]++;
>>
>> The cause is the following sequence in a multi threaded environment:
>> ImageProcessor ip = ...
>> ColorProcessor.setWeightingFactors(1, 0, 0);
>> ip.getHistogram();
>
>The ImageJ 1.48a daily build adds the non-static setRGBWeights() method
>that will allow you to avoid this exception by using:
>
>   ImageProcessor ip = ...
>   ((ColorProcessor)ip).setRGBWeights(1,0,0);
>   ip.getHistogram();

Thanks a lot for the quick help!

Franz

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html