转:http://www.c-sharpcorner.com/UploadFile/1d42da/threading-with-semaphore-in-C-Sharp/
Introduction
The semaphore class works similar to the Monitor and Mutex class but lets you set a limit on how many threads have access to a critical section. It's often described as a nightclub (the semaphore) where the visitors (threads) stands in a queue outside the nightclub waiting for someone to leave in order to gain entrance.
A critical section is a piece of code that accesses a shared resource (data structure or device) but the condition is that only one thread can enter in this section in a time.
Introducing : System.Threading.Semaphore
System.Threading.Semaphore provide all the method and property which are require to implement Semaphore.To use a semaphore in C#, you first need to instantiate an instance of a Semaphore object. The constructor, at a minimum, takes two parameters. The first is the number of resource slots initially available when the object is instantiated. The second parameter is the maximum number of slots available. If you want to reserve some slots for the calling thread, you can do so by making the first parameter smaller than the second. To reserve all slots for new threads, you should make both parameters the same.
After you instantiated your Semaphore object, you simply need to call the WaitOne() method when entering an area of code that you want restricted to a certain number of threads. When processing finishes, call the Release() method to release the slot back to the pool.
The count on a semaphore is decremented each time a thread enters the semaphore, and incremented when a thread releases the semaphore. When the count is zero, subsequent requests block until other threads release the semaphore. When all threads have released the semaphore, the count is at the maximum value specified when the semaphore was created.
Creating a new semaphore is accomplished through one of the existing constructors:
Using the code
using System;
using System.Threading;
namespace threadingSemaphore
{
class Akshay
{
static Thread[] threads =new Thread[10];
static Semaphore sem =new Semaphore(3, 3);
static void C_sharpcorner()
{
Console.WriteLine("{0} is waiting in line...",Thread.CurrentThread.Name);
sem.WaitOne();
Console.WriteLine("{0} enters the C_sharpcorner.com!",Thread.CurrentThread.Name);
Thread.Sleep(300);
Console.WriteLine("{0} is leaving the C_sharpcorner.com",Thread.CurrentThread.Name);
sem.Release();
}
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
threads[i] = new Thread(C_sharpcorner);
threads[i].Name = "thread_" + i;
threads[i].Start();
}
Console.Read();
}
}
}
Output
Some more important facts about Semaphore