使用Semaphore(旗语)控制线程同步

今天同事做培训讲到了线程同步的方法, 其中一个就是用到Semaphore。 Semaphore可以用在不同进程之间的线程同步, 若是在单进程中的线程同步, 使用lock或是Monitor就足够了。 正如其名 旗语就是存在多个任务, 每个任务上都插一个小旗作为互斥信号, 当有一个线程去访问该任务时, 就拔掉小旗 以示其他线程不能访问, 访问完成后再插回小旗使其他线程可以访问。

Demo:

 

代码
 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4 using  System.Threading;
 5 using  System.Diagnostics;
 6
 7 namespace  ConsoleApplication1
 8 ExpandedBlockStart.gifContractedBlock.gif {
 9
10    class Resources
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        public string Name;
13
14        public bool isInUse;
15    }

16    class Program
17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
18        public static Semaphore m_Semaphore;
19        public static List<Resources> m_ResourceList = new List<Resources>();
20        static Random m_Random = new Random();
21
22        public static void ThreadFunc()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
24            while (true)
25ExpandedSubBlockStart.gifContractedSubBlock.gif            {
26                int resourceID = 0;
27                m_Semaphore.WaitOne();
28
29                for (int i = 0; i < m_ResourceList.Count; i++)
30ExpandedSubBlockStart.gifContractedSubBlock.gif                {
31                    lock (m_ResourceList[i])
32ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
33                        if (!m_ResourceList[i].isInUse)
34ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
35                            resourceID = i;
36                            m_ResourceList[i].isInUse = true;
37                            break;
38                        }

39                    }

40                }

41                Console.WriteLine(string.Format("Thread {0} is using Resource {1}", Thread.CurrentThread.Name, m_ResourceList[resourceID].Name));
42
43                Thread.Sleep(m_Random.Next(10* 100);
44
45                lock (m_ResourceList[resourceID])
46ExpandedSubBlockStart.gifContractedSubBlock.gif                {
47                    m_ResourceList[resourceID].isInUse = false;
48                }

49
50                m_Semaphore.Release();
51            }

52
53        }

54
55        static void Main(string[] args)
56ExpandedSubBlockStart.gifContractedSubBlock.gif        {
57            m_Semaphore = new Semaphore(55);
58            //Add five resources to the resource list
59            for (int i = 0; i < 5; i++)
60ExpandedSubBlockStart.gifContractedSubBlock.gif            {
61                Resources r = new Resources();
62                r.Name = string.Format("{0}", i + 1);
63                r.isInUse = false;
64                m_ResourceList.Add(r);
65            }

66
67            //New 6 threads to access the resources list
68            for (int i = 0; i < 6; i++)
69ExpandedSubBlockStart.gifContractedSubBlock.gif            {
70                Thread th = new Thread(ThreadFunc);
71                th.Name = string.Format("{0}", i + 1);
72                th.Start();
73            }

74        }

75    }

76}

77
78

 

转载于:https://www.cnblogs.com/qixue/archive/2009/11/28/1612531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值