SoFunction
Updated on 2025-03-07

Simple example of using C# semaphores

This article describes the usage of C# semaphores. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
/*
  * Title: Sample code for how to use semaphores
  * Author:kagula
  * Date:2015-6-16
  * Environment: VS2010SP1, .NET Framework 4 client profile, C#.
  * Note:[1] "Semaphore" can be regarded as "authorization (certificate) pool".
  * There are zero or more authorizations (certificates) in a pool of authorizations.
  * [2] The following example sem of Semaphore is equivalent to an authorization pool with at most one authorization (certificate).
  * [3]Add an authorization (certificate) every time you call.
  * If the connection call multiple times causes the number of authorizations (certificates) that the authorization pool can accommodate, an exception will be thrown.
  * [4] One authorization (certificate) is used for each call.
  * */
namespace kagula
{
  class mySemaphore
  {
    //The first parameter represents the current number of authorizations.    // 0 means there is no authorization (certificate).    //The second parameter means that the Semaphore instance can accommodate up to several authorization certificates.    // 1 means that the maximum number of authorizations is 1.    // Exceed the allowed number of authorizations, for example, if two consecutive calls are called, an exception will be thrown.    public static Semaphore sem = new Semaphore(0, 1);
    public static void Main()
    {
      //Add an authorization once.      //Release a () block.      ();
      myThread mythrd1 = new myThread("Thrd #1");
      myThread mythrd2 = new myThread("Thrd #2");
      myThread mythrd3 = new myThread("Thrd #3");
      myThread mythrd4 = new myThread("Thrd #4");
      ();
      ();
      ();
      ();
      //input any key to continue...
      ();
    }//end main function
  }//end main class
  class myThread
  {
    public Thread thrd;
    public myThread(string name)
    {
      thrd = new Thread();
       = name;
      ();
    }
    void run()
    {
      ( + "Waiting for a permit(certificate)……");
      //If no parameters are added, it will cause infinite waiting.      if ((1000))
      {
        ( + "Apply for a license(certificate)……");
        (500);
        //Although the license is added below, other threads may not have obtained the license and exited timeout.        ( + "Add a license(certificate)……");
        ();
      }
      else
      {
        ( + " time out(After waiting for a while, I still haven't obtained the license(certificate))quit……");
      }
    }
  }//end class
}//end namespace

For more information about C# related content, please check out the topic of this site:Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.