/// <summary> /// Creates a new instance of the TaskDistributor. /// </summary> /// <param name="workerThreadCount">The number of worker threads, a value below one will create ProcessorCount x3 worker threads.</param> /// <param name="autoStart">Should the instance auto start the worker threads.</param> public TaskDistributor(int workerThreadCount, bool autoStart) : base() { if (workerThreadCount <= 0) { workerThreadCount = UnityEngine.SystemInfo.processorCount * 3; } workerThreads = new TaskWorker[workerThreadCount]; lock (workerThreads) { for (var i = 0; i < workerThreadCount; ++i) { workerThreads[i] = new TaskWorker(this); } } if (mainTaskDistributor == null) { mainTaskDistributor = this; } if (autoStart) { Start(); } }
/*! \endcond */ private void EnsureHelper() { if (dispatcher == null) { dispatcher = new Threading.Dispatcher(); } if (taskDistributor == null) { taskDistributor = new Threading.TaskDistributor(); } }
void OnDestroy() { foreach (var thread in registeredThreads) { thread.Dispose(); } if (dispatcher != null) { dispatcher.Dispose(); } dispatcher = null; if (taskDistributor != null) { taskDistributor.Dispose(); } taskDistributor = null; }
/// <summary> /// Disposes all TaskDistributor, worker threads, resources and remaining tasks. /// </summary> public override void Dispose() { while (true) { TaskBase currentTask; lock (taskQueue) { if (taskQueue.Count != 0) { currentTask = taskQueue.Dequeue(); } else { break; } } currentTask.Dispose(); } lock (workerThreads) { for (var i = 0; i < workerThreads.Length; ++i) { workerThreads[i].Dispose(); } workerThreads = new TaskWorker[0]; } dataEvent.Close(); dataEvent = null; if (mainTaskDistributor == this) { mainTaskDistributor = null; } }
public TaskWorker(TaskDistributor taskDistributor) : base(false) { this.TaskDistributor = taskDistributor; this.Dispatcher = new Dispatcher(false); }