September 18, 2010 by Christoff Truter C# Threading
In previous posts we had a quick look at the Mutex / Monitor classes, which limit our
apps to only allow one thread to access a resource (critical section) at a time.
But what about scenarios where we work with a pool of resources? In scenarios where we
need to limit the number of threads that uses a pool of resources.
One solution would be to simply modify a mutex/monitor to almost act like traffic
control. Observe the following rather dodgy snippet that attempts to solve this problem
(don't use)
using System;
using System.Threading;
using System.Linq;
public class Program
{
static int _maximumThreads = 3;
static object locker = new object();
static void WaitOne()
{
while (true)
{
lock (locker)
{
if (_maximumThreads > 0)
{
_maximumThreads--;
break;
}
}
}
}
static void Release()
{
lock (locker)
{
_maximumThreads++;
}
}
public static void Worker()
{
WaitOne();
Console.WriteLine("{0} Enters", Thread.CurrentThread.Name);
Thread.Sleep(3000);
Console.WriteLine("{0} Exists", Thread.CurrentThread.Name);
Release();
}
public static void Main(string[] args)
{
for (int i = 0; i < 8; i++)
{
Thread thread = new Thread(Worker);
thread.Name = String.Concat("Thread ", i + 1);
thread.Start();
}
}
}
using System;
using System.Threading;
using System.Linq;
public class Program
{
private static Semaphore _resourcePool;
private static int _maximumThreads = 3;
public static void Main(string[] args)
{
_resourcePool = new Semaphore(0, _maximumThreads);
for (int i = 0; i < 8; i++)
{
Thread thread = new Thread(Worker);
thread.Name = String.Concat("Thread ", i + 1);
thread.Start();
}
_resourcePool.Release(_maximumThreads);
}
private static void Worker()
{
_resourcePool.WaitOne();
Console.WriteLine("{0} Enters", Thread.CurrentThread.Name);
Thread.Sleep(3000);
Console.WriteLine("{0} Exits", Thread.CurrentThread.Name);
_resourcePool.Release();
}
}
July 23, 2013 by Anonymous
From what i 've see so far, this the best example ever. Thanks :)