2

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 } } } 
3
  • 2
    You need to explain a little more what you're trying to do and why this isn't doing what you expect. For one - do you want all 1000 threads to be acting on the same object (which probably doesn't make sense for 1000 threads to be doing), or are they supposed to be acting on potentially different objects? Commented Aug 11, 2010 at 20:52
  • threads working on same object Commented Aug 11, 2010 at 20:55
  • Why question voted to close ? Commented Aug 11, 2010 at 21:03

4 Answers 4

4

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.

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

5 Comments

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.
So what is your application showing you that indicates the lock isn't working?
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: what unexpected results? Did it slow down or crash?
@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.
3

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.

4 Comments

It doesn't make sense and still inspection what caused weird results
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: That is the default stack-size.
Problem is not about memory size.
2

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...)

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.