/// <summary>
 /// Create a thread pool.
 /// </summary>
 /// <param name="poolCount">How many threads in the pool. These will always be running.</param>
 /// <param name="workerTemplate">The shared thread worker that implements the work.</param>
 public ThreadedWorkerPool(int poolCount, IThreadableWorker workerTemplate)
 {
     Assert.IsFalse(_externalMarkedForDeath, "Pool is in an invalid state of death.");
     _externalMarkedForDeath = false;
     for (int i = 0; i < poolCount; i++)
     {
         Thread thread = new Thread(() => WorkThread(workerTemplate));
         thread.Start();
     }
 }
        private void WorkThread(IThreadableWorker worker)
        {
            int workCheckCooldown = worker.WorkCheckCooldownMilliseconds;

            Assert.IsTrue(workCheckCooldown >= 0, "Thread worker cooldown must be 0 milliseconds or greater.");
            while (!SynchronizedShouldDie())
            {
                bool readyToWork;
                lock (_externalThreadLock) {
                    readyToWork = _externalInputBuffer.Count > 0;
                }
                if (readyToWork)
                {
                    TI   input         = default(TI);
                    bool workAvilabale = false;
                    lock (_externalThreadLock) {
                        if (_externalInputBuffer.Count > 0)
                        {
                            input         = _externalInputBuffer.Dequeue();
                            workAvilabale = true;
                        }
                    }
                    if (workAvilabale)
                    {
                        ThreadedWorkProduct result = worker.ThreadedWork(input);
                        Assert.IsFalse(result.ResultType == ThreadedWorkResultType.FatalError,
                                       "A worker thread has experienced a FatalError. No recovery.");
                        if (result.ResultType == ThreadedWorkResultType.OutputProduced)
                        {
                            lock (_externalThreadLock)
                            {
                                _externalOutputBuffer.Enqueue(result.Output);
                            }
                        }
                    }
                }
                Thread.Sleep(workCheckCooldown);
            }
        }