0

I'm trying to use this great project but since i need to scan many images the process takes a lot of time so i was thinking about multi-threading it.
However, since the class that makes the actual processing of the images uses Static methods and is manipulating Objects by ref i'm not really sure how to do it right. the method that I call from my main Thread is:

public static void ScanPage(ref System.Collections.ArrayList CodesRead, Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types)
{
    //added only the signature, actual class has over 1000 rows
    //inside this function there are calls to other
    //static functions that makes some image processing
}

My question is if it's safe to use use this function like this:

List<string> filePaths = new List<string>();
        Parallel.For(0, filePaths.Count, a =>
                {
                    ArrayList al = new ArrayList();
                    BarcodeImaging.ScanPage(ref al, ...);
                });

I've spent hours debugging it and most of the time the results i got were correct but i did encounter several errors which i now can't seem to reproduce.

EDIT
I pasted the code of the class to here: http://pastebin.com/UeE6qBHx

9
  • 2
    without analyzing the method itself noone can tell you whether it is threadsafe! Commented May 21, 2013 at 19:28
  • 1
    The fact that the "great project" still uses the obsolete ArrayList class makes me concerned about its thread safety. Commented May 21, 2013 at 19:30
  • If it's using global variable (or variable not used as parameter or inside the function) then it's a no. Commented May 21, 2013 at 19:31
  • @JohnSaunders the project is old but does the job nicely, i'll change it to List<string> Commented May 21, 2013 at 19:34
  • @the_lotus is passing a parameter by ref considered a global variable? Commented May 21, 2013 at 19:36

2 Answers 2

1

I'm pretty sure it is thread safe. There are two fields, which are configuration fields and are not modified inside the class. So basically this class has no state and all calculation has no side effects (Unless I don't see something very obscure).

Ref modifier is not needed here, because the reference is not modified.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no way of telling unless you know if it stores values in local variables or in a field (in the static class, not the method).

All local variables will be fine and instanced per call, but the fields will not.

A very bad example:

public static class TestClass
{
    public static double Data;
    public static string StringData = "";

    // Can, and will quite often, return wrong values.
    //  for example returning the result of f(8) instead of f(5)
    //  if Data is changed before StringData is calculated.
    public static string ChangeStaticVariables(int x)
    {
        Data = Math.Sqrt(x) + Math.Sqrt(x);
        StringData = Data.ToString("0.000");
        return StringData;
    }

    // Won't return the wrong values, as the variables
    //  can't be changed by other threads.
    public static string NonStaticVariables(int x)
    {
        var tData = Math.Sqrt(x) + Math.Sqrt(x);
        return Data.ToString("0.000");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.