Mutex semaphore is Python

Concurrency and locking

  • a plurality of threads share data when, if the data is not protected, it may appear the number of data inconsistencies, the use of locks, semaphores, condition locks
    • b.
      • c. mutex
        1. mutex is a lock using the code protected to sacrifice performance in exchange for security code, then after Rlock have to
        relase unlock otherwise will lose the advantage of multithreaded programs
        2. mutually exclusive lock the basic use rules:
        . 1 Import Threading
         2 # statement mutex 
        . 3 = Lock threading.Rlock ();
         . 4 DEF handle (SID): # function implementation code 
        . 5 lock.acquire () # lock 
        . 6 # Writer codeing 
        . 7 lock.relase () # release lock
        8
        semaphore:
        1. Call relarse () call +1 semaphore will acquire () -1 semaphores
        A will be understood as critical to the use of resources, and determining the conditions for entering the critical section.
        2. semphore (): when calling when relarse () function does not check the simple semaphore +1 ceiling situation. First
        start parameter 0
        3. boudedsemphore (): When the amount of call boundary signal relarse () will be +1, and checks the semaphore
        limit situation. Not exceed the upper limit
        a. Budedsemaphore used to set the initial time of 0 is not allowed, an exception is thrown
        b. At least one setting, such as a consumer product when a variable to be set out as
        an amount, to make a judgment start time variable, so as not to use decisions acquier
        4. semaphore substantially apply:
        1 # statement Semaphore: 
        2 = threading.Semaphore SEMA (0); # No limit checking 
        . 3 SEMA = threading.BuderedSeamphore (1) # capped check the settings 
        . 4. 5 
        Apple = 1 
        . 6 DEF consumner ():
         . 7 seam.acquire ( ); # -1 
        . 8. 9 IF Apple ==. 1 :
         10 Pass 
        . 11 the else : sema2.release (); # +. 1 
        12 is DEF Product ():
         13 is seam.relarse (); # + 1'd 
        14 IF Apple. 1 == :
         15 Pass 
        16 the else :
        
        17 Print ( " Consumer: " , the Apple);
         18



      • All code:
      • # - * - Coding: UTF-8 - * - 
        "" " 
        the Created ON Mon Sep 9 21:49:30 2019 
        
        @author: DGW-PC 
        " "" 
        # semaphores solve the producer consumer problem 
        Import Random;
         Import Threading;
         Import Time; 
        
        # statement semaphore 
        SEMA = threading.Semaphore (0); # must be used to write parameter indicates the number of 0 
        sema2 threading.BoundedSemaphore = (. 1 ); 
        
        Apple =. 1 ; 
        
        DEF Product (): # Manufacturer 
            Global Apple; 
            Apple the random.randint = (1,100 ); 
            the time.sleep ( . 3 );
             Print ( " generation Apple:",apple);
            #sema2.release(); # +1
            if apple==1:
                 pass
            else: sema2.release();#+ 1 
                
        def consumer():
            print("等待");
            sema2.acquire();# -1
            if apple==1:
                pass
            else:        
                print("消费:",apple);
            
        
        threads=[];
        
        for i in range(1,3):
            t1=threading.Thread(target=consumer);
            t2=threading.Thread(target=product);
            t1.start();
            t2.start();
            threads.append(t1);
            threads.append(t2);
        for x in threads:
            x.join();
        
            
            

         

Guess you like

Origin www.cnblogs.com/dgwblog/p/11494915.html