信号量(semaphore)不过是由内核维护的 int32变量而已,(说通俗点就是好比一个线程容器里面允许执行的线程数,0计数就是允许执行的0个线程数,1就是允许执行的1个线程数,2就是允许执行的2个线程数,等等一次类推。。。。。。,0就是当前执行的线程数占满了容器没空余的了)

    当信号量为0时,在信号量上等待的线程会全部阻塞;

    当信号量大于0时,就解除阻塞。

  在一个信号量上等待的一个线程解除阻塞时,内核自动从信号量的计数中减1,线程运行完后调用Release,计数就加1。信号量还关联了一个最大的Int32值,当前计数绝不允许超过最大计数。下面展示了semaphore类的样子: 


 public sealed class Semaphore :WaitHandle{
//初始化信号量:初始请求数为initialCount,最大请求数为maximumCount
public Semaphore(Int32 initialCount,Int32 maximumCount);
public Int32 Release();//调用Release(1);返回上一个计数
public Int32 Release(Int32 releaseCount);//返回上一个计数
}
initialCount:就是初始化信号量时,解除阻塞的线程数。

maximumCount:就是信号量允许执行的最大线程数。
 

  一个自动重置事件在行为上和最大计数为1的信号量非常相似。两者的区别在于,可以在一个自动重置事件上连续多次调用Set,同时仍然只有一个线程解除阻塞。相反,在一个信号量上连续多次调用Release,会使它的内部计数一直递增,这可能解除大量线程的阻塞。如果在一个信号量上多次调用Release,会导致它的计数超过最大计数,这是Release会抛出一个SemaphoreFullException.

  请看MSDN中的一个例子:

  

 using System;
using System.Threading; public class Example
{
// A semaphore that simulates a limited resource pool.
//
private static Semaphore _pool; // A padding interval to make the output more orderly.
private static int _padding; public static void Main()
{
// Create a semaphore that can satisfy up to three
// concurrent requests. Use an initial count of zero,
// so that the entire semaphore count is initially
// owned by the main program thread.
//初始化时允许解除的阻塞线程数量为0,最大的执行线程数为3,所以在第执行完第44行代码之前,开启的子线程中的代码都不会执行,如果注释掉第45行代码,这5个子线程都不会执行,如果改成:new Semaphore(1,3)初始化时允许解除的阻塞线程数量为1,最大执行的线程数为3,所以在执行完31行代码后会有一个子线程在运行的,如果注释掉第45行代码,就会只有一个信号量慢慢的运行完这5个子线程
_pool = new Semaphore(, ); // Create and start five numbered threads.
//
for (int i = ; i <= ; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(Worker)); // Start the thread, passing the number.
//
t.Start(i);
} // Wait for half a second, to allow all the
// threads to start and to block on the semaphore.
//
Thread.Sleep(); // The main thread starts out holding the entire
// semaphore count. Calling Release(3) brings the
// semaphore count back to its maximum value, and
// allows the waiting threads to enter the semaphore,
// up to three at a time.
//
Console.WriteLine("Main thread calls Release(3).");
_pool.Release();//此句代码的通俗意思就是重新再增加3个可以执行的线程,如果初始化new semaphore(1,5)运行完这代代码就是1+3=4个线程数解除阻塞了,如果初始化为new semaphore(0,5)运行完这段代码就是0+3=3个线程数解除阻塞了。
//Console.WriteLine("_pool {0}", _pool.Release());
//Console.WriteLine("_pool {0}", _pool.Release());
//Console.WriteLine("_pool {0}", _pool.Release());
//Console.WriteLine("_pool {0}", _pool.Release());
//Console.WriteLine("_pool {0}", _pool.Release()); Console.WriteLine("Main thread exits."); // Thread.Sleep(5000);
// Console.WriteLine("_pool {0}", _pool.Release()); Console.ReadLine();
} private static void Worker(object num)
{
// Each worker thread begins by requesting the
// semaphore.
Console.WriteLine("Thread {0} begins " +
"and waits for the semaphore.", num);
_pool.WaitOne(); // A padding interval to make the output more orderly.
int padding = Interlocked.Add(ref _padding, ); Console.WriteLine("Thread {0} enters the semaphore.", num); // The thread's "work" consists of sleeping for
// about a second. Each thread "works" a little
// longer, just to make the output more orderly.
//模拟阻塞线程
Thread.Sleep(1000 + padding); Console.WriteLine("Thread {0} releases the semaphore.", num);
Console.WriteLine("Thread {0} previous semaphore count: {1}",
num, _pool.Release());
}
}
运行的其中之一的一个结果为:
Thread  begins and waits for the semaphore.
Thread begins and waits for the semaphore.
Thread begins and waits for the semaphore.
Thread begins and waits for the semaphore.
Thread begins and waits for the semaphore.
Main thread calls Release().
Thread enters the semaphore.
Thread releases the semaphore.
Thread previous semaphore count:
Thread enters the semaphore.
Thread releases the semaphore.
Thread previous semaphore count:
Thread enters the semaphore.
Thread releases the semaphore.
Thread previous semaphore count:
Main thread exits.
Thread enters the semaphore.
Thread releases the semaphore.
Thread previous semaphore count: 1 //执行这段代码前。信号量(semaphore)中还有两个线程在执行(当前运行运行的线程和下面还没运行完的一个线程,所以剩余1个线程的信号量(semaphore)可以执行.
Thread enters the semaphore.
Thread releases the semaphore.
Thread previous semaphore count: 2 //执行这段代码前。信号量(semaphore)中有2个空闲的位置,可以执行线程了。

 这只是自己的理解,不正确的望各位大侠们指教。。。。。。。。。。。。。。。

最新文章

  1. android 编译过程
  2. [k]自定义样式下拉菜单
  3. CLR环境中内置了几个常用委托(转)
  4. poj 2153
  5. 简介AngularJS中使用factory和service的方法
  6. 。【自学总结 1】------3ds Max 界面
  7. oracle管理控制台不能打开,提示此网站的安全证书有问题?
  8. JavaScript函数的四种存在形态
  9. html 上传文件
  10. 关于oracle后导数据的一些小TIPS
  11. MongoDB学习教程(2)-常用命令
  12. c/c++ 表达式求值
  13. 【XSY1515】【GDKOI2016】小学生数学题 组合数学
  14. [日常] Go语言圣经-字节切片与字符串
  15. vba 调用 countif 函数问题
  16. linux_check
  17. hdu1693
  18. 河工大第十届ACM省赛随笔
  19. gulp.src()内部实现探究
  20. urllib2特点--超时设置

热门文章

  1. 在 Windows 上使用 Cygwin
  2. NET中调用存储过程(Output、Input)
  3. PHP版本VC6与VC9/VC11/VC14、Thread Safe与None-Thread Safe等的区别
  4. Python中的偏函数
  5. 获取 user-agents
  6. JQuery基础(选择器、事件、DOM操作)
  7. 退出循环break,在while、for、do...while、循环中使用break语句退出当前循环,直接执行后面的代码。
  8. [转]Excel.dll 导出Excel控制
  9. 使用xmlHttprequest有感
  10. 利用新浪js接口根据ip地址获取实际地址