Class SharedPool<T>
Provides a pool of shared objects. Use this class in conjunction with Shared<T>.
Namespace: Microsoft.Psi
Assembly: Microsoft.Psi.dll
Syntax
public class SharedPool<T> : IDisposable where T : class
Type Parameters
Name | Description |
---|---|
T | The type of the objects managed by this pool. |
Constructors
View SourceSharedPool(Func<T>, Int32, KnownSerializers)
Initializes a new instance of the SharedPool<T> class.
Declaration
public SharedPool(Func<T> allocator, int initialSize = 10, KnownSerializers knownSerializers = null)
Parameters
Type | Name | Description |
---|---|---|
Func<T> | allocator | The allocation function for constructing a new object. |
System.Int32 | initialSize | The initial size of the pool. The size will be adjusted up as needed, but never down. |
KnownSerializers | knownSerializers | An optional set of known serializers. Only required if the pool holds objects that are deserialized from an older store. |
Properties
View SourceAvailableCount
Gets the number of objects available, i.e., that are not live, in the pool.
Declaration
public int AvailableCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
TotalCount
Gets the total number of objects managed by this pool.
Declaration
public int TotalCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
View SourceDispose()
Releases all unused objects in the pool.
Declaration
public void Dispose()
GetOrCreate()
Attempts to retrieve an unused object from the pool if one is available, otherwise creates and returns a new instance.
Declaration
public Shared<T> GetOrCreate()
Returns
Type | Description |
---|---|
Shared<T> | An unused object, wrapped in a ref-counted Shared<T> instance. |
Reset(Boolean)
Resets the shared pool.
Declaration
public void Reset(bool clearLiveObjects = false)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | clearLiveObjects | Indicates whether to clear any live objects. |
Remarks
If the clearLiveObjects flag is false, an exception is thrown if a reset is attempted while the pool still contains live objects.
TryGet(out T)
Attempts to retrieve an unused object from the pool.
Declaration
public bool TryGet(out T recyclable)
Parameters
Type | Name | Description |
---|---|---|
T | recyclable | An unused object from the pool, if there is one. |
Returns
Type | Description |
---|---|
System.Boolean | True if an unused object was available, false otherwise. |
TryGet(out Shared<T>)
Attempts to retrieve an unused object from the pool.
Declaration
public bool TryGet(out Shared<T> recyclable)
Parameters
Type | Name | Description |
---|---|---|
Shared<T> | recyclable | An unused object, wrapped in a ref-counted Shared<T> instance. |
Returns
Type | Description |
---|---|
System.Boolean | True if an unused object was available, false otherwise. |