I'm looking for someone that might know more about this, my gut tells me that the answer is "no, it is not thread-safe" but I want to be sure.
In order to illustrate my question, I have provided some context with this class
public class MyContext
{
private readonly object _lock = new object();
public delegate bool MyDelegate(MyContext context);
private MyDelegate _multicastDelegate;
public MyContext()
{
_multicastDelegate = null;
}
public void AddDelegate(MyDelegate del)
{
lock(_lock)
{
_multicastDelegate += del;
}
}
public void RemoveDelegate(MyDelegate del)
{
lock(_lock)
{
_multicastDelegate += del;
}
}
public void Go()
{
_multicastDelegate.Invoke(this);
}
}
Edit: I added locks to the example above, but it's really not the point of my question.
I am trying to understand better if the array that holds the invocation list is thread-safe. Frankly, I am not clearly seeing how this all gets put together and some help would be appreciated.
According to documentation I have found, the only quote that provides no real insight is the following:
A MulticastDelegate has a linked list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.
https://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx
Thanks in advance.
Delegates are immutable. You never change a delegate. Any method that appears to mutate a delegate is in fact creating a new instance.
Delegates are immutable; once created, the invocation list of a delegate does not change.
There is thus no cause for concern that the invocation list may be updated whilst a delegate is being invoked.
What you do have to guard against, however, and have failed to do in your method is when the delegate may in fact be null
.
(new MyContext()).Go();
will cause an exception. You used to have to guard against this by reading the value into a local variable, testing it for null and then invoking using it. It can now more easily be resolved as:
public void Go()
{
_multicastDelegate?.Invoke(this);
}