Home > Backend Development > C++ > How Can I Implement a Thread-Safe and Customizable Object Pooling Pattern in C#?

How Can I Implement a Thread-Safe and Customizable Object Pooling Pattern in C#?

Barbara Streisand
Release: 2025-01-02 12:57:39
Original
513 people have browsed it

How Can I Implement a Thread-Safe and Customizable Object Pooling Pattern in C#?

C# Object Pooling Pattern Implementation

Introduction

Object pooling is a technique used to optimize performance by reusing expensive or frequently allocated objects instead of creating and destroying them frequently. In this C# implementation, we explore how to create a robust and thread-safe object pool with customizable settings.

Implementation

Resource Loading Strategy

The object pool supports both eager loading (creating all objects upfront) and lazy loading (creating objects only when needed). A LoadingMode enumeration defines these options.

Access Strategy

The access pattern determines how objects are selected from the pool. The implementation includes three options:

  • FIFO (First-In, First-Out): Objects are retrieved in the order they were inserted.
  • LIFO (Last-In, First-Out): Objects are retrieved in the reverse order they were inserted.
  • Circular: Objects are retrieved in a round-robin fashion, ensuring even distribution of access.

These options are specified through the AccessMode enumeration.

Class Structure

The Pool class manages the object pool. It contains an IItemStore interface to handle access strategy. Different implementations of IItemStore are used for each access mode.

For lazy loading, two modes are provided:

  • Lazy: Only creates a new object when the pool has no available objects.
  • LazyExpanding: Creates new objects until the pool reaches its maximum capacity, then switches to lazy mode.

Usage

To use the object pool, create a Pool instance with the desired settings. The Acquire() method retrieves an available object. The Release() method returns an object to the pool.

Pooled Object Proxy

To simplify usage and avoid direct access to the Pool class, a PooledFoo class is introduced. It proxies the IFoo interface and automatically releases the underlying Foo object to the pool when disposed.

Multithreading and Isolation

The Semaphore class is used to ensure thread-safe access to the inner item store, preventing multiple threads from acquiring or releasing the same object simultaneously.

Additional Features

The pool can be preloaded with a specified number of objects during initialization. It also provides a IsDisposed property to determine if the pool is no longer active, in which case it cleans up all remaining pooled objects.

Example Usage

// Create the object pool
Pool<IFoo> pool = new Pool<IFoo>(
    PoolSize,
    p => new PooledFoo(p),
    LoadingMode.Lazy,
    AccessMode.Circular
);

// Acquire an object from the pool
using (IFoo foo = pool.Acquire())
{
    // Use the object
}
Copy after login

Conclusion

This implementation provides a flexible and thread-safe object pooling pattern that can be customized to suit various application requirements. By reusing objects, you can improve performance and reduce resource consumption, especially for frequently allocated or expensive objects.

The above is the detailed content of How Can I Implement a Thread-Safe and Customizable Object Pooling Pattern in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template