Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C# 4.0

Concurrent Object Pool, the Right Way

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
22 Apr 2013CPOL2 min read 37.5K   1.1K   34  
An implementation of a generic, concurrent object pool with smart memory management.
#region Copyright (c) 2012 Roman Atachiants
/*************************************************************************
 * 
 * Copyright (C) 2012 Roman Atachiants
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 * OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace ObjectPool
{
    /// <summary>
    /// Defines a contract for an object pool.
    /// </summary>
    public interface IRecycler
    {
        /// <summary>
        /// Acquires an instance of a recyclable object
        /// </summary>
        /// <returns></returns>
        IRecyclable Acquire();

        /// <summary>
        /// Releases an instance of a recyclable object back to the pool.
        /// </summary>
        /// <param name="instance">The instance of IRecyclable to release.</param>
        void Release(IRecyclable instance);

        /// <summary>
        /// Gets the overall number of elements managed by this pool.
        /// </summary>
        int Count { get; }

        /// <summary>
        /// Gets the number of available elements currently contained in the pool.
        /// </summary>
        int AvailableCount {get;}

        /// <summary>
        /// Gets the number of elements currently in use and not available in this pool.
        /// </summary>
        int InUseCount {get;}

    }

    /// <summary>
    /// Defines a contract for an object pool.
    /// </summary>
    public interface IRecycler<T> : IRecycler
        where T : class, IRecyclable
    {
        /// <summary>
        /// Acquires an instance of a recyclable object
        /// </summary>
        /// <returns></returns>
        new T Acquire();

        /// <summary>
        /// Releases an instance of a recyclable object back to the pool.
        /// </summary>
        /// <param name="instance">The instance of IRecyclable to release.</param>
        void Release(T instance);
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Misakai Ltd.
Ireland Ireland
Roman Atachiants, Ph.D. is the architect behind emitter.io service, a real-time, low-latency publish/subscribe service for IoT, Gaming. He is a software engineer and scientist with extensive experience in different computer science domains, programming languages/principles/patterns & frameworks.

His main expertise consists of C# and .NET platform, game technologies, cloud, human-computer interaction, big data and artificial intelligence. He has an extensive programming knowledge and R&D expertise.



Comments and Discussions