1

Im trying to implement multi-threading in my application that makes a lot of float number calculations (neural network).

I wrote function that makes necessary calculations and updates array outside that function. My actual, single-thread code looks like this (simplified for better understanding):

class MyClass
{
    // this array after all calculations should contain 
    // result of only one calculation, 
    // that returned smallest value in one of array fields 
    // (smallest neural network error)
    float[] bestResult;

    // runs calculations on all "sets of data"
    public void findBestResult(void)
    {
        foreach (something...) // data rows from database cached in one array
        {
            calculate(x[]);
        }
    }

    // calculates one "set of data"
    public void calculateAndUpdateResultIfBetter(float[] inputData)
    {

        if (bestResult[0] > calculatedData[0])
             bestResult = calculatedData; // update only if condition is true
    }

}

Im low level programmer, i don't know how to use advanced (?) .NET threading techniques, that use Synchronize etc. I know how to create one additional thread for something and update some controls on form by using delegates.

I have no idea how to work with 2-8 threads doing exactly same thing and competing with each other.

The question 1 is - can you help me with this? I don't know how to start. SOLVED BY Niko Drašković

EDIT: The question 2 is - will lock() method lock my array for read and write?

2
  • Have you seen lock? Commented Oct 21, 2012 at 23:17
  • No, i didn't. It seems to be my answer, but i need some more tips. I add additional question. Commented Oct 21, 2012 at 23:26

1 Answer 1

5

You use the lock statement to prevent different threads to run the same code at the same time. You would need a reference to use as an identifier for the lock. It's common to create a simple object that is used only for the locking:

float[] bestResult;
object sync = new Object();

Then around the code that accesses the array you use lock:

lock (sync) {
  if (bestResult[0] > calculatedData[0]) {
    bestResult = calculatedData;
  }
}

You might want to have each thread first calculate the best value that it can see within the data that it is responsible for, and then combine those best values. If you run the locked code too often you will make the threads wait for each other, losing a lot of the reason for running separate threads in the first place.

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

5 Comments

Hmmm in your code bestResult is not inside sync object. Is this right?
@Kamil: Yes. The object reference is only used as an identifier so that all the threads use the same monitor for the locking. The lock only keeps the threads from entering the same code block at the same time, it doesn't protect the data in the variable that you use in the lock statement.
Where i should put that lock? In my calculations thread? Or somewhere in main? Now when you wrote about locking code block to run by multiple threads i don't know... When i put that in thread method it will be not the same code block?
I thought it locks variable, but when you wrote about locking code block... I am not sure how it works now.
@Kamil: While you have more than one thread running, you need to put locks around every code block that accesses the variable in those threads. The syntax of the lock statement can easily lead you to believe that it protects the variable from being accessed from somewhere else, but that's not how it works. The reference is only used as an identifier for the lock, it doesn't matter what the reference points to, but it has to keep its value. If you would use the bestResult variable as lock identifier, the lock would not work as you are changing the reference.

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.