Take the 2-minute tour ×

It is possible to execute the following code from multiple threads simultaneously.

this._sequencer.Completed += OnActivityFinished;

Is it thread safe to add delegate to an event handler from multiple threads?

Is it thread safe to remove delegate to from event handler from multiple threads?

What is the simplest and maintainable way of making this thread safe?

share|improve this question
add comment

3 Answers

up vote 16 down vote accepted

If you don’t specify your own event add/remove handlers, the C# compiler generates this add handler (reconstructed by .NET Reflector):

public void add_MyEvent(EventHandler value)
    EventHandler handler2;
    EventHandler myEvent = this.MyEvent;
        handler2 = myEvent;
        myEvent = Interlocked.CompareExchange<EventHandler>(ref this.MyEvent, handler3, handler2);
    while (myEvent != handler2);

and a remove handler that looks the same but with Delegate.Remove instead of Delegate.Combine.

Notice the use of Interlocked.CompareExchange? This prevents a race condition between updating the event’s backing field and reading from it. Thus, it is thread-safe.

share|improve this answer
A small clarification- this is the implementation in .NET 4, Before that it used lock(this) (also see desco's answer) –  Ohad Schneider Aug 4 '11 at 11:52
@ohadsc I just created an event and compile against .net 2 and it still created the Interlocked code as defined by timwi –  Simon Apr 15 '12 at 22:47
@Simon you're right, it's a compiler feature, not a runtime one. I should have said "this is the implementation in the C# 4 compiler" –  Ohad Schneider Apr 16 '12 at 8:22
add comment

It depends on the implementation of the event, to be honest.

The field-like events generated by the C# compiler are thread-safe, but if it's a custom event, who knows?

Note that in a multi-threaded app you should expect a race condition between adding/removing a handler and the event firing... for example, the event may start to fire, you could then unsubscribe, and your handler would still be called after that unsubscription.

share|improve this answer
add comment

for field-like events adding/removing of handlers is thread-safe. From spec:

When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and creates accessors for the event that add or remove event handlers to the delegate field. In order to be thread-safe, the addition or removal operations are done while holding the lock (§8.12) on the containing object for an instance event, or the type object (§ for a static event.

However it is true for C# 3.0 and lesser, in C# 4.0 compiler generates lock-free implementation using Interlocked routines (but spec remains the same - bug?)

In custom implementations no one can tell exactly... except maybe the author of code :)

share|improve this answer
I believe the spec has been updated, but the updated version hasn't been published yet. –  Jon Skeet Aug 19 '10 at 13:53
maybe, I was referring to version available here (publish date 4/19/2010) - microsoft.com/downloads/… –  desco Aug 19 '10 at 13:54
add comment

Your Answer


