C # Semaphore Usage Simple Example _c# tutorial

Source: Internet
Author: User
Tags semaphore

The example in this article describes the C # semaphore usage. Share to everyone for your reference, specific as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
 * * Title: How to use the Semaphore sample code * Author:kagula * date:2015-6-16 * environment:vs2010sp1,. NET Framework 4 Client Profile, C #.
 * Note:[1] "semaphore" can be considered as "authorization (card) pool".
 * One authorization (card) pool has 0 or more authorizations (certificates).
 * [2] The following sample SEM of semaphore is equivalent to a maximum of one authorization (certificate) pool. * [3] Every time the SEM is called.
 Release to add an authorization (certificate). * Connection calls multiple SEM.
 Release causes an exception to be thrown that exceeds the number of authorizations (certificates) that can be accommodated by the authorization pool. * [4] Every time the SEM is called.
 WaitOne Use an Authorization (certificate).
    * */namespace Kagula {class Mysemaphore {///first parameter, representing the current authorization count.
    0 indicates that there is no authorization (certificate).
    The second parameter, representing the semaphore instance, holds a maximum of several licenses.
    1 indicates a maximum authorized number of 1 times. exceeds the allowable number of authorizations, such as SEM.
    Release is called two consecutive times and throws an exception.
    public static semaphore sem = new semaphore (0, 1);
      public static void Main () {//Add one authorization. Release a SEM.
      Blocking of WaitOne (). Sem.
      Release ();
      Mythread mythrd1 = new Mythread ("Thrd #1");
      Mythread mythrd2 = new Mythread ("Thrd #2");
      Mythread mythrd3 = new Mythread ("Thrd #3"); Mythread MythrD4 = new Mythread ("Thrd #4");
      Mythrd1.thrd.Join ();
      Mythrd2.thrd.Join ();
      Mythrd3.thrd.Join ();
      Mythrd4.thrd.Join ();
      Input any key to continue ...
    Console.readkey ();
    }//end Main function}//end main class class Mythread {public Thread thrd;
      Public Mythread (string name) {thrd = new Thread (This.run); Thrd.
      name = name; Thrd.
    Start (); } void Run () {Console.WriteLine (THRD).
      Name + "is waiting for a license (card) ...");
      If you do not add a parameter, it can cause infinite wait. if (MySemaphore.sem.WaitOne (1000)) {Console.WriteLine (THRD).
        Name + "Apply to license (certificate) ...");
        Thread.Sleep (500);
        Although the license is added below, other threads may not have permission to exit the timeout. Console.WriteLine (THRD.
        Name + "Add a license (card) ...");
      MySemaphore.sem.Release (); else {Console.WriteLine (THRD).
      Name + "Timeout (waiting for a period of time or not getting a license) to exit ...");

 }}}//end class}//end namespace

Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.