Linux semaphore (semaphore) and mutual exclusion (mutex)

In multi-threaded programming, for various reasons, we will use a variety of mechanisms such as semaphores or locks on some of the operations are controlled, there will tell linux C programming, commonly used in two ways: semaphores mode and lock mode

Lock: used for mutual exclusion to protect a resource between multiple threads can only be accessed by a plurality of threads in the moment, for a process of

Semaphore: used for synchronization among multiple threads of execution for securing according to the established order of the steps, a multi-threaded process may be used in a said plurality of processes may also be used

wxy: Lock to protect a resource, from the moment locked, if not of resources (multiple threads can access those variables or call a global variable, or class member variables and so on ...?), you can still go on

          Semaphores for synchronization from the semaphore that moment, not active unless another wanted to give me a signal (post), you can continue to say, and I just continue to go with

A: Mutex (lock)

1, usage example (after test)

// 0. header incorporated 
#include <the mutex> // 1. define lock the mutex mutex_me;
 ++++++ thread 1 +++++++++ +++++++++ thread 2+ ++++++++ // 2. the                                       // by 2 on    
mutex_me. Lock () mutex_me. Lock () / * step1 * / / * step1 * / / * resources * / / * step2 * / // 3. unlock                                                                        
mutex_me.unlock ()                                 / * resources * / / *





step2*/ //3.解锁
mutex_me.unlock()
 

 

 

2, the semaphore

1. Usage example (untested)

// 0. header incorporated 
#include <semaphore.h> // 1. Definitions / Create the semaphore sem_t semaphoretcpItem; 
sem_init ( & semaphoretcpItem, 0 , 1 ); ++++++ Thread 1 +++++++ thread 2 ++ +++++++++ +++++++++ @ 2 wait semaphore                                                 // 3. release semaphore 
of sem_wait (& semaphoretcpItem);) sem_post (& semaphoretcpItem); / * step1 * / / * step2 * / / * resources * / / * step3 * /








                                                                                                     
                                                               
                                                     

 2, the correlation function analytic

sem_init (): used to initialize a semaphore.

Its prototype: extern int sem_init __P ((sem_t * __ sem, int __pshared, unsigned int __value));

                        sem semaphore is a pointer to a structure;

                        pshared not zero semaphore shared between processes, or only shared by all threads in the current process;

                        value is given an initial value of the semaphore.

sem_post (sem_t * sem): used to increase the value of the semaphore.

                        When a thread is blocked on the semaphore, one thread calling this function causes which is not blocked by the selection mechanism is also a thread scheduling policy decisions. 

sem_wait (sem_t * sem): is used to block the current thread until the semaphore value is greater than 0 sem, released after the value minus one blocking sem, show reduced by the use of the common resources.

sem_trywait (sem_t * sem): a function of sem_wait () nonblocking version, which directly sem semaphore value minus one.

sem_destroy (sem_t * sem): to release the semaphore sem.

Guess you like

Origin www.cnblogs.com/shuiguizi/p/11530531.html