Highly there is a problem in this lock but i couldn’t understand what is that. I have strong suspicious that below example doesn’t lock enough well. So what can be problem ?
class example
{
object locker = new object();
void start()
{
for (int i = 0; i < 1000; i++)
{
(new Thread(dostuff)).Start();
}
}
void dostuff()
{
lock (locker)
{
//dosomething
}
}
}
Your code creates 1000 Threads. That is enormously expensive, requiring over 1 GB of memory.
And then all those threads compete for a single lock, essentially serialzing (de-threading) the whole operation.
But your lock works fine, nothing wrong there. It’s just that when you run this application it might look like your PC is crashing.
Also note that the object you are trying to protect should be tied 1-on-1 with the locker object.
But for a better answer you’ll have to post code that is a little more complete and maybe closer to the real thing.