C# delegates are immutable, so when you add or remove a function from the delegate, it is replaced by a new object.
Considering the following piece of code:
lock(shared_instance.my_delegate){
shared_instance.my_delegate+=Foo;
}
Consider the following situation:
Threads 1 and 2 arrive at the lock, thread 2 blocks on the delegate instance, let's call it A
.
Thread 1 replaces A
with a new instance, B
, and exits the lock.
Does thread 2 acquire lock on B
or on A
? If it's on A
, then it seems to me that thread 3 could come along, acquire a lock on B
at the same time when thread 2 acquires a lock on A
, and they could simultaneously try to overwrite the deleage and a race would occur, correct?
EDIT:
I now realize that the question could be generalized to any reference type:
lock(shared_ref){
shared_ref=different_ref;
}
Basically what I want to know is: If this happens, waiting threads will still lock on the old reference, yes?