views:

107

answers:

4

Hello

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
        }
    }
}
+4  A: 

It looks like you're spinning up 1000 threads, then locking each one. That means that the code within dostuff (that lives in the lock section) will execute sequentially instead of at the same time.

As written, it would be more efficient to just call dostuff() directly in the for loop.

RQDQ
It is just an example to demonstrate my spesific problem. It is already expected lock but my application shows that lock doesn't work so i get unexpected results.
Freshblood
So what is your application showing you that indicates the lock isn't working?
RQDQ
I don't wantt to tell spesific side of my codes but i can not understand what cause the unexpected results in my application. There is just one instance of example class so written code has no problem right ?
Freshblood
@Freshblood: what unexpected results? Did it slow down or crash?
Henk Holterman
@Henk Holterman - doesn't slow or crash but getting weird results.Getting results makes me feel that there is a problem on lock. But as u said there is nothing wrong on lock so i will look another else what can cause a problem.
Freshblood
+2  A: 

I'm not quite sure what you are doing here, but your 1000 threads will run serially. Defeats the purpose of threads in the first place.

Your "dostuff" method immediate locks for the entire length of the thread run. The thread then unlocks, which the next thread in line can begin processing (and locks...)

Starkey
+2  A: 

I'm not sure what your issue is-- this code will start dostuff 1000 times, but the code within the lock will only execute one at a time.

The only problem you might have with the lock is if you need to execute the code only one at a time regardless of the number of copies of the example class you have created. If this is important, then you need to make the locker object static.

Russ
+3  A: 

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.

Henk Holterman
It doesn't make sense and still inspection what caused weird results
Freshblood
I'm not sure where you gather this requiring over 1 GB of memory. It's only 1,000 threads, so your assuming he is acquiring at minimum 1.02 MB per thread?
Gary L Cox Jr
@Gary: That is the default stack-size.
Henk Holterman
Problem is not about memory size.
Freshblood