views:

55

answers:

1

I created an object that has specific method to process different kind of events and will lived until my application is running. I also created individual delegates pointing to each methods and are referenced to x number of threads. Given y and z thread will invoke method1 by using a delegate pointing to it, will z thread wait for y thread to finish before it can use method1 ? or they can invoke it simultaneously?

+2  A: 

Unless you are employing locking or some other kind of mutex, each thread will invoke the method behind the delegate in parallel. They will not wait for each other.

This is where thread-safety comes into play. There is nothing inherently bad about two different threads executing a method at the same time, but if that method touches instance members of its owner class and both of these threads are working against a single instance at the same time, you risk thread collisions and bugs ranging from fatal errors to strange, subtle, and difficult-to-reproduce behaviors. My rule of thumb is don't mix threads and state when I can avoid it.

Rex M
Thanks for the updates. If I'm going to provide some lock mechanism that is a part of the object being processed something like SyncRoot object, does this make the other threads to wait to other threads? or the only threads that will wait will be the threads that are processing the same object?
powerbox