Synchronization and mutual exclusion between the Linux thread

      While concurrently executing threads, we need to ensure secure access to critical resources, competition for resources to prevent the thread, resulting in data ambiguity.

      

      Thread Synchronization: Variable Conditions

      Why use condition variables?

      The timing of the critical resources of controllability , conditions will notify other threads waiting for the operation of critical resources, similar signals. Scene: T-DAY lined up to see the show / producer-consumer model

      What condition variables?

      A synchronization mechanism, a thread is variable so as to modify the conditions for the other thread to continue down to meet the other threads have the reception signal condition change occurs.      

      Operating condition variables?

      Initialization and destruction

        pthread_cond_wait

      Condition is not met will be released and blocked waiting for the lock , this function is atomicity operations : a queue waiting condition into the thread     2 releases the lock                            

      Conditions are met thread wakes up and locked

         pthread_cond_signal one wake     

             Wake up a thread waiting in the queue

      pthread_cond_broadcast broadcast wake

               Wake up all threads waiting in the queue       

      Why wait and need to unlock atomic operations / Why do you want to use a mutex condition variable?

      Because pthread_cond_wait locks in order to protect the variable conditions, to prevent miss signal, if not the wait unlock atomicity operations, such as thread A to unlock, and the CPU time slice switching to the thread B, B thread lock condition variable signal and transmits this when then switch to the thread a, thread a had a chance to wait for the missed signals, it may permanently blocked it. Therefore, waiting and unlock operations must be atomic.

      Why do we need a while loop to determine whether there is a critical resource?

      In many under the circumstances, the producer sends a signal, the waiting thread wakes up and locked, but only one thread can lock, other threads will be blocked waiting for the lock, if the thread runs out of critical resources, no other threads judge to continue to go down, it is unreasonable.

      singnal first unlocked or unlocked?

      If to unlock the lock is no thread to block waiting to get, and then use a critical resource, singal no sense after unlocking, that is false wake ;

      First singal wake up, wake up to let the competition thread lock in linux, there are two queues, one cond_wait, one mutex_lock, singal just let threads on cond_wait transferred to mutex_lock, will not return to user space. This improves effectiveness.

      

      

      The thread mutually exclusive: mutex

      Why use a mutex?

      For the same time the only access to critical resources , protection of critical resources to prevent modification. Scene: cattle to grab votes

      What mutex lock is?

      Is a 0/1 counter , it represents resources are operable, 0 representing no resource can be operated.

      Mutex operation?

      Initialization and destruction

      If the count is locked --- 1, equal to 0, the operation required; if the count is 0, the block waiting count becomes 1

      Count is set to 1 unlocking ---

      

      

Guess you like

Origin www.cnblogs.com/Duikerdd/p/11761151.html
Recommended