Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace R_01.classes
- {
- public class hose<T>
- where T : class, new()
- {
- private Queue<T> objectPool;
- public int count;
- public int numFree;
- private bool lockObjects;
- public hose()
- {
- objectPool = new Queue<T>();
- lockObjects = false;
- numFree = 0;
- count = 0;
- }
- public T get()
- {
- while (lockObjects && !gameData.exiting) { }
- lockObjects = true;
- T obj;
- lock (objectPool)
- {
- try
- {
- if (numFree > 0)
- {
- obj = objectPool.Dequeue();
- numFree--;
- if (obj == null)
- {
- obj = new T();
- count++;
- }
- }
- else
- {
- obj = new T();
- count++;
- }
- }
- catch (Exception ex)
- {
- ex.ToString();
- obj = new T();
- count++;
- }
- finally
- {
- lockObjects = false;
- }
- }
- return obj;
- }
- public void put(T obj)
- {
- while (lockObjects && !gameData.exiting) { }
- lockObjects = true;
- lock (objectPool)
- {
- try
- {
- objectPool.Enqueue(obj);
- numFree++;
- }
- catch (Exception ex)
- {
- ex.ToString();
- }
- finally
- {
- lockObjects = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement