//From main thread private void StartWork(WorkerBase worker, WaitCallback workerLogic, WorkerBase.WorkerDataBase data) { var localWorksCount = Interlocked.Increment(ref _worksCount); if (localWorksCount > _maxWorks) { _maxWorks = localWorksCount; } _totalWorks++; int threadId; lock (_pool) { threadId = Array.FindIndex(_pool, w => w == null); if (threadId < 0) { throw new InvalidOperationException("There is no free thread in pool"); } _pool[threadId] = worker; } data.ThreadId = threadId; #if MAIN_THREAD_WORK workerLogic(data); //Debug work from Main thread #else ThreadPool.QueueUserWorkItem(workerLogic, data); #endif }
private void FinishWork(WorkerBase.WorkerDataBase data) { Interlocked.Decrement(ref _worksCount); lock (_pool) { if (_pool[data.ThreadId] == null) { Debug.LogError("Thread in pool is empty already"); } _pool[data.ThreadId] = null; } }