c# Semaphore(信号量)

2023年1月29日11:27:37

参考:
http://blog.csdn.net/feilongzaitianhehe/article/details/51460529
http://www.cnblogs.com/tianzhiliang/archive/2010/08/31/1813635.html


信号量说简单点就是为了线程同步,或者说是为了限制线程能运行的数量。

那它又是怎么限制线程的数量的哩?是因为它内部有个计数器,比如你想限制最多5个线程运行,那么这个计数器的值就会被设置成5,如果一个线程调用了这个Semaphore,那么它的计数器就会相应的减1,直到这个计数器变为0。这时,如果有另一个线程继续调用这个Semaphore,那么这个线程就会被阻塞。

获得Semaphore的线程处理完它的逻辑之后,你就可以调用它的Release()函数将它的计数器重新加1,这样其它被阻塞的线程就可以得到调用了。


类似互斥锁,但它可以允许多个线程同时访问一个共享资源

通过使用一个计数器来控制对共享资源的访问,如果计数器大于0,就允许访问,如果等于0,就拒绝访问。计数器累计的是“许可证”的数目,为了访问某个资源。线程必须从信号量获取一个许可证。

通常在使用信号量时,希望访问共享资源的线程将尝试获取一个许可证,如果信号量的计数器大于0,线程将获取一个许可证并将信号量的计数器减1,否则先线程将阻塞,直到获取一个许可证;当线程不再需要共享资源时,将释放锁拥有的许可证,并将许可证的数量加1,如果有其他的线程正在等待许可证,那么该线程将立刻获取许可证。

另外,允许同时访问的资源的进程数量是在创建信号量时指定的,如果创建一个允许进程访问的信号量数目为1,则该信号量就和互斥锁的用法一样。

        //
        // 摘要:
        //     初始化 System.Threading.Semaphore 类的新实例,并指定初始入口数和最大并发入口数。
        //
        // 参数:
        //   initialCount:
        //     可以同时授予的信号量的初始请求数。
        //
        //   maximumCount:
        //     可以同时授予的信号量的最大请求数。
        //
        // 异常:
        //   T:System.ArgumentException:
        //     initialCount 大于 maximumCount。
        //
        //   T:System.ArgumentOutOfRangeException:
        //     maximumCount 为小于 1。- 或 -initialCount 小于 0。
        [SecuritySafeCritical]
        public Semaphore(int initialCount, int maximumCount);

获取许可证使用WaitOne();

不需要时释放使用 public int Release(),或者public int Release(int releaseCount);


实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class mythread
    {
        public Thread m_thread;
        static Semaphore m_semaphore = new Semaphore(2, 2);

        public mythread(string name)
        {
            m_thread = new Thread(this.run);
            m_thread.Name = name;
            m_thread.Start();
        }

        void run()
        {
            Console.WriteLine(m_thread.Name + ":正在等待一个许可证...");
            //申请一个许可证
            m_semaphore.WaitOne();
            Console.WriteLine(m_thread.Name + ":申请到许可证...");

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(m_thread.Name + ":" + i);
                Thread.Sleep(1000);
            }

            Console.WriteLine(m_thread.Name + ":释放许可证...");
            m_semaphore.Release();
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            MySemaphore();
        }

        static void MySemaphore()
        {
            mythread thread1 = new mythread("Thrd # 1 ");
            mythread thread2 = new mythread("Thrd # 2 ");
            mythread thread3 = new mythread("Thrd # 3 ");
            mythread thread4 = new mythread("Thrd # 4 ");
            thread1.m_thread.Join();
            thread2.m_thread.Join();
            thread3.m_thread.Join();
            thread4.m_thread.Join();
        }
    }
}

运行结果:

c# Semaphore(信号量)


  • 作者:Spades_J
  • 原文链接:https://blog.csdn.net/fuemocheng/article/details/78215734
    更新时间:2023年1月29日11:27:37 ,共 2240 字。