by

C# Non Blocking, Multithreaded, Maker/Taker Queue

effn sweet....
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/541435/C--Non-Blocking--Multithreaded--Maker-Ta?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public class KQue { readonly object synk = new object(); Thread[] wrkrz; Queue<Action> itmQ = new Queue<Action>(); public KQue (int workerCount) { wrkrz = new Thread [workerCount]; for (int i = 0; i < workerCount; i++) { (wrkrz[i] = new Thread(konsume)).Start(); } } public void Shutdown (bool waitingFor) { foreach (var thd in wrkrz) { NQItem (null); } if (waitingFor) { foreach (var thd in wrkrz) { thd.Join(); } } } public void NQItem (Action itm) { lock (synk) { itmQ.Enqueue (itm); Monitor.Pulse (synk); } } private void konsume() { while (true) { Action itm; lock (synk) { while (itmQ.Count == 0) { Monitor.Wait (synk); } itm = itmQ.Dequeue(); } if (itm == null) { return; } itm(); } } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus