/// <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) { if (workerThreadCount <= 0) { #if !NO_UNITY workerThreadCount = UnityEngine.SystemInfo.processorCount * 3; #else workerThreadCount = Environment.ProcessorCount * 3; #endif } 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(); } }
private void EnsureHelperInstance() { if (CurrentDispatcher == null) { CurrentDispatcher = new Dispatcher(); } if (CurrentTaskDistributor == null) { CurrentTaskDistributor = new TaskDistributor(); } }
void OnDestroy() { foreach (var thread in registeredThreads) { thread.Dispose(); } if (CurrentDispatcher != null) { CurrentDispatcher.Dispose(); } CurrentDispatcher = null; if (CurrentTaskDistributor != null) { CurrentTaskDistributor.Dispose(); } CurrentTaskDistributor = null; }
/// <summary> /// Disposes all TaskDistributor, worker threads, resources and remaining tasks. /// </summary> public override void Dispose() { while (true) { TaskBase currentTask; lock (TaskList) { if (TaskList.Count != 0) { currentTask = TaskList[0]; TaskList.RemoveAt(0); } else { break; } } currentTask.Dispose(); } lock (workerThreads) { foreach (TaskWorker t in workerThreads) { t.Dispose(); } workerThreads = new TaskWorker[0]; } DataEvent.Close(); DataEvent = null; if (mainTaskDistributor == this) { mainTaskDistributor = null; } }
/// <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) { if (workerThreadCount <= 0) { #if !NO_UNITY workerThreadCount = UnityEngine.SystemInfo.processorCount * 3; #else workerThreadCount = Environment.ProcessorCount * 3; #endif } 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(); }
/// <summary> /// Disposes all TaskDistributor, worker threads, resources and remaining tasks. /// </summary> public override void Dispose() { while (true) { TaskBase currentTask; lock (TaskList) { if (TaskList.Count != 0) { currentTask = TaskList[0]; TaskList.RemoveAt(0); } else break; } currentTask.Dispose(); } lock (workerThreads) { foreach (TaskWorker t in workerThreads) t.Dispose(); workerThreads = new TaskWorker[0]; } DataEvent.Close(); DataEvent = null; if (mainTaskDistributor == this) mainTaskDistributor = null; }
public TaskWorker(TaskDistributor taskDistributor) : base(false) { TaskDistributor = taskDistributor; Dispatcher = new Dispatcher(false); }
private void EnsureHelperInstance() { if (CurrentDispatcher == null) CurrentDispatcher = new Dispatcher(); if (CurrentTaskDistributor == null) CurrentTaskDistributor = new TaskDistributor(); }