Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a boost::shared_ptr<T> equivalent in C#?

Tags:

c#

Just curious, I've been using boost:shared_ptr's in the past a LOT - for having multiple objects store a shared pointer to a single object etc.

Is there an equivalent of this functionality in C#?

like image 439
Maciek Avatar asked Dec 02 '22 06:12

Maciek


2 Answers

boost::shared_ptr allows for reference counted pointers in an environment which is not garbage collected. The .NET runtime allows for full garbage collection, so there is no need for a reference counting pointer container - simply store your object references.

like image 118
Yann Ramin Avatar answered Dec 23 '22 00:12

Yann Ramin


The GC eliminates the need for shared pointers when it comes to memory management, but it doesn't do this for resource management.

IDisposable is .NET's solution for resource managemenet, but it doesn't allow shared ownership semantics. See this article for a thorough discussion of the weaknesses.

Here's a simple implementation of a SharedRef, which follows the boost::shared_ptr pattern of a heap allocated reference count object. Not sure how useful it will be yet, but feel free to comment and improve it...

/// <summary>
/// SharedRef class, which implements reference counted IDisposable ownership.
/// See also the static helper class for an easier construction syntax.
/// </summary>
public class SharedRef<T> : IDisposable
    where T:class,IDisposable
{
    private SharedRefCounter<T> _t;

    /// <summary>
    /// Create a SharedRef directly from an object. Only use this once per object.
    /// After that, create SharedRefs from previous SharedRefs.
    /// </summary>
    /// <param name="t"></param>
    public SharedRef(T t)
    {
        _t = new SharedRefCounter<T>(t);
        _t.Retain();
    }

    /// <summary>
    /// Create a SharedRef from a previous SharedRef, incrementing the reference count.
    /// </summary>
    /// <param name="o"></param>
    public SharedRef(SharedRef<T> o)
    {
        o._t.Retain();
        _t = o._t;
    }

    public static SharedRef<T> Create(T t)
    {
        return new SharedRef<T>(t);
    }

    private bool _disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        if (disposing)
        {
            if (_t != null)
            {
                _t.Release();
                _t = null;
            }
        }

        _disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
    }

    public T Get()
    {
        return _t.Get();
    }
}

/// <summary>
/// Static helper class for easier construction syntax.
/// </summary>
public static class SharedRef
{
    /// <summary>
    /// Create a SharedRef directly from an object. Only use this once per object.
    /// After that, create SharedRefs from previous SharedRefs.
    /// </summary>
    /// <param name="t"></param>
    public static SharedRef<T> Create<T>(T t) where T : class,IDisposable
    {
        return new SharedRef<T>(t);
    }

    /// <summary>
    /// Create a SharedRef from a previous SharedRef, incrementing the reference count.
    /// </summary>
    /// <param name="o"></param>
    public static SharedRef<T> Create<T>(SharedRef<T> o) where T : class,IDisposable
    {
        return new SharedRef<T>(o);
    }
}

/// <summary>
/// Class which holds the reference count for a shared object.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SharedRefCounter<T> where T : class,IDisposable
{
    private int _count;
    private readonly T _t;

    public T Get()
    {
        return _t;
    }

    public SharedRefCounter(T t)
    {
        _count = 0;
        _t = t;
    }

    /// <summary>
    /// Decrement the reference count, Dispose target if reaches 0
    /// </summary>
    public void Release()
    {
        lock (_t)
        {
            if (--_count == 0)
            {
                _t.Dispose();
            }
        }
    }

    /// <summary>
    /// Increment the reference count
    /// </summary>
    public void Retain()
    {
        lock (_t)
        {
            ++_count;
        }
    }
}

Notes:

  1. To ensure there's only one reference count for each shared object, make sure you only create 1 SharedRef directly from the object, and after that, create new SharedRefs from previous SharedRefs. This is the same for boost::shared_ptr. It would be nice to add some protection to the class in case you forget this.
  2. It seems a shame that the SharedRef itself has to be a reference type, allocated on the heap, but I think this is the only way of making it Disposable.
  3. I haven't implemented the equivalent of weak_ptr yet, but I think that could easily be added.
  4. It's thread safe (I think), but could probably be more efficient as it uses locks.

Here's some test code showing it in action across 3 threads.

[TestFixture]
public class SharedRefTest
{
    public class MyDisposable : IDisposable
    {
        private bool _disposed = false;
        private string _id;

        public MyDisposable(string id) { _id = id; }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    Console.WriteLine("{0}: Disposing {1}", Thread.CurrentThread.ManagedThreadId, _id);
                }

                _disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        public override string ToString()
        {
            return _id;
        }
    }

    [Test]
    public void Run()
    {
        Task t1, t2, t3;

        // create 2 objects
        Console.WriteLine("{0}: starting initial scope", Thread.CurrentThread.ManagedThreadId);
        using (var o1 = SharedRef.Create(new MyDisposable("o1")))
        using (var o2 = SharedRef.Create(new MyDisposable("o2")))
        {
            // and 3 sharedrefs, which will be Disposed in 3 separate threads
            var p1 = SharedRef.Create(o1);
            var p2a = SharedRef.Create(o2);
            var p2b = SharedRef.Create(o2);
            t1 = Task.Run(() =>
            {
                using (p1)
                {
                    Console.WriteLine("{0}: in another thread, using o1", Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                    Console.WriteLine("{0}: in another thread, exiting scope", Thread.CurrentThread.ManagedThreadId);
                }
            });
            t2 = Task.Run(() =>
            {
                using (p2a)
                {
                    Console.WriteLine("{0}: in another thread, using o2", Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                    Console.WriteLine("{0}: in another thread, exiting scope", Thread.CurrentThread.ManagedThreadId);
                }
            });
            t3 = Task.Run(() =>
            {
                using (p2b)
                {
                    Console.WriteLine("{0}: in another thread, using o2", Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                    Console.WriteLine("{0}: in another thread, exiting scope", Thread.CurrentThread.ManagedThreadId);
                }
            });
            Console.WriteLine("{0}: exiting initial scope", Thread.CurrentThread.ManagedThreadId);
        }
        t1.Wait();
        t2.Wait();
        t3.Wait();
    }
}
like image 35
ben Avatar answered Dec 23 '22 00:12

ben