Menu

#5 Not thread safe

open
nobody
None
5
2011-01-31
2011-01-31
No

The code in Helper.GetRandomOrder:

int randomPosition = random.Next(size);
int temp = randomOrder[i];
randomOrder[i] = randomOrder[randomPosition];
randomOrder[randomPosition] = temp;

aborts with and Index Out Of Range error when running multiple threads.

It works fine when running one thread.

Discussion

  • Frank Willett

    Frank Willett - 2011-02-01

    Sorry for this posting, I have tracked the source of the error to System.Random in mono. I found a FastRandom on CodeProject and it solved the problem.

     
  • Sean Dawson

    Sean Dawson - 2011-09-08

    I also have this problem, and it had nothing to do with mono. The .NET Random class is not type safe, and when a single instance is used by multiple threads it can malfunction and begin to only return 0's which in turn freezes the neural network learning. I fixed the problem by implimenting a fix demonstrated at http://blogs.msdn.com/b/pfxteam/archive/2009/02/19/9434171.aspx, which involves using [ThreadStatic] parameter to create a new random instance for each thread. Using this method will most likely not affect single core performance, but will avoid confusion when people use NeuronDotNet with multiple cores. As far as I can tell only the helper.cs file needs to be modified and all the code will be typesafe, insofar that each thread has it's own instance of a neural network. I could submit my fix but I have no idea how open source projects work, I just thought I'd let you know. Contact me if you are interested.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.