码迷,mamicode.com
首页 > 编程语言 > 详细

C#多线程开发10:线程同步之Semaphore类

时间:2015-05-26 12:45:52      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:semaphore   semaphoreslim   信号量   semaphore.wait   semaphore.release   

Semaphore类表示信号量

信号量和互斥类似,只是信号量可以同时由多个线程使用,而互斥只能由一个线程使用。也就是说,使用信号量时,可以多个线程同时访问受保护的资源。下面实例演示了“学生到食堂就餐”的场景,一共有10个学生需要就餐,但是食堂每次只能接纳4名学生就餐,所以将信号量的计数设置为4,每次有4个任务(就餐任务)可以获得锁定。剩下的学生就必须等待,等到锁定被解除时,学生才可以继续获得锁定,进入食堂就餐。

using System;
using System.Threading;
using System.Threading.Tasks;
namespace SemaphoreExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int studentCount = 10;
            int seatCount = 4;//小小食堂只有4个位子
            var semaphore = new SemaphoreSlim(seatCount, seatCount);
            var eatings = new Task[studentCount];
            for (int i = 0; i < studentCount; i++)
            {
                eatings[i] = Task.Run(() => Eat(semaphore)); 
            }
            Task.WaitAll(eatings);
            Console.WriteLine("All students have finished eating!");
        }
        static void Eat(SemaphoreSlim semaphore)
        {
            semaphore.Wait();
            try
            {
                Console.WriteLine("Student {0} is eating now!", Task.CurrentId);
                Thread.Sleep(1000);
            }
            finally
            {
                Console.WriteLine("Student {0} have finished eating!", Task.CurrentId);
                semaphore.Release();
            }
        }
    }
}

代码的执行结果如下图所示。

 技术分享

使用Semaphore类的实例方法Wait()锁定信号量,使用Release()方法可以解除锁定。

.Net中信号量有两个类:Semaphore和SemaphoreSlim。Semaphore类可以命名,允许在不同进程之间同步。SemaphoreSlim类是对于较短等待时间进行了优化的轻型版本。本文中使用的是SemaphoreSlim类。

C#多线程开发10:线程同步之Semaphore类

标签:semaphore   semaphoreslim   信号量   semaphore.wait   semaphore.release   

原文地址:http://blog.csdn.net/yl2isoft/article/details/46005569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!