• (C#) 线程操作 限制可同时访问某一资源或资源池的线程数。Semaphore 类。Mutex类


    信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确、合理的使用公共资源。
    Semaphore分为单值和多值两种,前者只能被一个线程获得,后者可以被若干个线程获得。
    以一个停车场是运作为例。为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这时如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入一辆,如果又离开两辆,则又可以放入两辆,如此往复。
    在这个停车场系统中,车位是公共资源,每辆车好比一个线程,看门人起的就是信号量的作用。
    更进一步,信号量的特性如下:信号量是一个非负整数(车位数),所有通过它的线程(车辆)都会将该整数减一(通过它当然是为了使用资源),当该整数值为零时,所有试图通过它的线程都将处于等待状态。在信号量上我们定义两种操作: Wait(等待) 和 Release(释放)。 当一个线程调用Wait(等待)操作时,它要么通过然后将信号量减一,要么一直等下去,直到信号量大于一或超时。Release(释放)实际上是在信号量上执行加操作,对应于车辆离开停车场,该操作之所以叫做“释放”是因为加操作实际上是释放了由信号量守护的资源。
     
    class Program
        {
            // 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.
                //
                _pool = new Semaphore(0, 3);
    
                // Create and start five numbered threads. 
                //
                for (int i = 1; i <= 5; i++)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(Worker));
    
                    // Start the thread, passing the number.
                    //
                    t.Start(i);
                }
    
                // Wait for a second, to allow all the
                // threads to start and to block on the semaphore.
                //
                Thread.Sleep(1000);
    
                // 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);
    
                Console.WriteLine("Main thread exits.");
                Console.Read(); 
            }
    
            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, 500);
    
                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());
            }
        }

    互斥体,又名同步基元Mutex类用于进程间同步。

    http://msdn.microsoft.com/zh-cn/library/system.threading.mutex(v=vs.100).aspx

  • 相关阅读:
    eBay商家五大市场调研工具
    最新Android ADT, SDK, SDK_tool等官方下载说明
    Android——关于ListView控件的使用方法
    Auto CAD的相关问题·绘图单位
    淘宝数据分析之数据分析工具的三个需求层次
    电阻器阻值标识的相关知识
    Android软件开发之ListView 详解
    解决android模拟器划动手势时自动输入字母“c” 的问题
    争辩:关于输出大电流的移动电源给小电流的手机充电的问题
    关于C51的中断函数要注意的几个问题
  • 原文地址:https://www.cnblogs.com/fdyang/p/3026012.html
Copyright © 2020-2023  润新知