# HG changeset patch
# User cin
# Date 2014-06-19 16:04:20
# Node ID 90069a2ec20aa8630765d546d6e2073b59ef0e39
# Parent 10c7337d29e76b8ac13e0b09718552b9cf9af860
Added SafePool
diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj
--- a/Implab/Implab.csproj
+++ b/Implab/Implab.csproj
@@ -92,6 +92,7 @@
+
diff --git a/Implab/SafePool.cs b/Implab/SafePool.cs
new file mode 100644
--- /dev/null
+++ b/Implab/SafePool.cs
@@ -0,0 +1,40 @@
+using Implab.Parallels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace Implab {
+ public class SafePool where T : new() {
+ readonly MTQueue m_queue = new MTQueue();
+ readonly int m_size;
+ int m_count = 0;
+
+ public SafePool() : this(10) {
+
+ }
+
+ public SafePool(int size) {
+ Safe.ArgumentInRange(size,1,size,"size");
+
+ m_size = size;
+ }
+
+ public T Allocate() {
+ T instance;
+ if (m_queue.TryDequeue(out instance)) {
+ Interlocked.Decrement(ref m_count);
+ return instance;
+ }
+ return new T();
+ }
+
+ public void Release(T instance) {
+ if (m_count < m_size) {
+ Interlocked.Increment(ref m_count);
+ m_queue.Enqueue(instance);
+ }
+ }
+ }
+}