What is the correct way of adding thread-safety to an IDisposable object?

Ivaylo Slavov picture Ivaylo Slavov · Jan 19, 2012 · Viewed 14k times · Source

Imagine an implementation of the IDisposable interface, that has some public methods.

If an instance of that type is shared between multiple threads and one of the threads may dispose it, what is the best way to ensure the other threads do not attempt to work with the instance after disposed? In most cases, after the object is disposed, its methods must be aware of it and throw the ObjectDisposedException or maybe InvalidOperationException or at least inform the calling code for doing something wrong. Do I need synchronization for every method - particularly around the check if it is disposed? Do all IDisposable implementations with other public methods need to be thread-safe?


Here is an example:

public class DummyDisposable : IDisposable
{
    private bool _disposed = false;

    public void Dispose()
    {
        _disposed = true;
        // actual dispose logic
    }

    public void DoSomething()
    {
        // maybe synchronize around the if block?
        if (_disposed)
        {
            throw new ObjectDisposedException("The current instance has been disposed!");
        }

        // DoSomething logic
    }

    public void DoSomethingElse()
    {
         // Same sync logic as in DoSomething() again?
    }
}

Answer

Dan Rigby picture Dan Rigby · Jan 19, 2012

The simplest thing you can do is mark the private disposed variable as volatile and inspect it at the beginning of your methods. You can then throw an ObjectDisposedException if the object has already been disposed.

There are two caveats to this:

  1. You shouldn't throw an ObjectDisposedExceptionif the method is an event handler. Instead you should just gracefully exit from the method if that is possible. The reason being is that there exists a race condition where events can be raised after you unsubscribe from them. (See this article by Eric Lippert for more information.)

  2. This doesn't stop your class from being disposed while you are in the middle of executing one of your class methods. So if your class has instance members that can't be accessed after disposal, you're going to need to setup some locking behaviour to ensure access to these resources are controlled.

Microsoft's guidance around IDisposable says you should check for disposed on all methods, but I haven't personally found this necessary. The question really is, is something going to throw an exception or cause unintended side effects if you allow a method to execute after the class is disposed. If the answer is yes, you need to do some work to make sure that doesn't happen.

In terms of whether all IDisposable classes should be thread safe: No. Most of the use cases for disposable classes involve them only ever being accessed by a single thread.

That being said, you may want to investigate why you need your disposable class to be thread safe as it adds a lot of additional complexity. There may be an alternate implementation that allows you to not have to worry about thread safety issues in your disposable class.