I need code like this (little mockup completely ignoring CPU load costs)
The code currently just does not work, it works fine until another thread calls Add() and everything dead locks. (it's called from within a delegate called in DoStuffWithArray())
Code: Select all
public myClass() {
new Thread(new ThreadStart() { while(true) { DoStuffWithArray(); })).Start();
}
private myObject[] array = new myObject[0];
void Add(myObject arr) {
lock(array) {
//add to array
}
}
void DoStuffWithArray() {
lock(array) {
//do stuff with every object
//clear object list
array = new myObject[0]; //I think this would cause it?
}
}
Also, this needs to be high performance code and I'm aware of the performance lost by using "lock" but I have to sacrifice performance for correct syncronization.
I have also tried but still does'nt work...
Code: Select all
static object l = new object();
void lockMe() { lock(l) { } }
Please help! Could it be a gliche with the actual v2.0 .Net framework? (System.Monitor class)