// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; namespace E { public class Ressource { private const int MS_RESOURCE_WORK = 10; private Random rng = new Random(); public void Use() { Thread.Sleep(rng.Next(MS_RESOURCE_WORK)); } } public class RessourcePool { private BlockingCollection pool; public RessourcePool(int poolsize) { pool = new BlockingCollection(new ConcurrentQueue(), poolsize); for(int i = 0; i < poolsize; i++) { pool.Add(new Ressource()); } } public Ressource Acquire() { return pool.Take(); } public void Release(Ressource res) { pool.Add(res); } } public abstract class Task { public abstract void Run(RessourcePool pool); } public class StopTask : Task { public override void Run(RessourcePool pool) { throw new Exception("STOP"); } } public class NormalTask : Task{ private const int MS_NON_RESOURCE_WORK = 1; private Random rng = new Random(); public override void Run(RessourcePool pool) { Thread.Sleep(rng.Next(MS_NON_RESOURCE_WORK)); Ressource res = pool.Acquire(); res.Use(); pool.Release(res); Thread.Sleep(rng.Next(MS_NON_RESOURCE_WORK)); } } public class WorkerThread { private BlockingCollection tasks; private RessourcePool pool; private Thread realthread; public WorkerThread(BlockingCollection tasks, RessourcePool pool) { this.tasks = tasks; this.pool = pool; realthread = new Thread(Run); } public void Run() { while(true) { Task t = tasks.Take(); if(t is StopTask) { tasks.Add(t); break; } t.Run(pool); } } public void Start() { realthread.Start(); } public void WaitForComplete() { realthread.Join(); } } public class WorkerProcess { private WorkerThread[] threads; public WorkerProcess(int nthreads, int poolsize, BlockingCollection tasks) { this.threads = new WorkerThread[nthreads]; for(int i = 0; i < nthreads; i++) { threads[i] = new WorkerThread(tasks, new RessourcePool(poolsize)); } } public void Start() { for(int i = 0; i < threads.Length; i++) { threads[i].Start(); } } public void WaitForComplete() { for(int i = 0; i < threads.Length; i++) { threads[i].WaitForComplete(); } } } public class System { private WorkerProcess[] procs; public System(int nprocs, int nthreads, int poolsize, BlockingCollection tasks) { procs = new WorkerProcess[nprocs]; for(int i = 0; i < nprocs; i++) { procs[i] = new WorkerProcess(nthreads, poolsize, tasks); } } public void Start() { for(int i = 0; i < procs.Length; i++) { procs[i].Start(); } } public void WaitForComplete() { for(int i = 0; i < procs.Length; i++) { procs[i].WaitForComplete(); } } } public class Workload : BlockingCollection { public Workload(int ntasks) { for(int i = 0; i < ntasks; i++) { Add(new NormalTask()); } Add(new StopTask()); } } public class Program { public static void Test(string lbl, int nprocs, int nthreads, int poolsize) { System sys = new System(nprocs, nthreads, poolsize, new Workload(100000)); DateTime dt1 = DateTime.Now; sys.Start(); sys.WaitForComplete(); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} : {1}", lbl, (dt2 - dt1).TotalSeconds); } public static void Main(string[] args) { Test("1p & 20t", 1, 20, 50); Test("1p & 100t", 1, 100, 50); Test("1p & 500t", 1, 500, 50); Test("5p & 20t", 5, 4, 10); Test("5p & 100t", 5, 20, 10); Test("5p & 500t", 5, 100, 10); } } }