protected void balanceQueues(object sender, JobEmptyQueueArgs e) { if (totalCount() < refillThreshold) { e.Quit = true; return; } var newQueue = new MaybeQueue <Action <int> >(); for (var i = 0; i < processorCount; i++) { while (queue.Dequeue(i).If(out var action)) { newQueue.Enqueue(action); } } queue.ResetCurrentAffinity(); while (newQueue.Dequeue().If(out var item)) { queue.Enqueue(item); } e.Quit = false; }
public void Dispatch(JobQueue queue) { manualResetEvent.Reset(); var thread = new Thread(() => { try { var coreMask = getCoreMask(affinity); setProcessorAffinity(coreMask); while (true) { while (queue.Dequeue(affinity).If(out var action)) { try { action(affinity); } catch (Exception exception) { JobException?.Invoke(this, new JobExceptionArgs(affinity, exception)); } Thread.Sleep(500); } lock (locker) { var args = new JobEmptyQueueArgs(affinity); EmptyQueue?.Invoke(this, args); if (args.Quit) { break; } } } } finally { manualResetEvent.Set(); } }); thread.Start(); }