Linux environment programming-thread synchronization [mutual exclusion lock]

1. Synchronization concept

In life, our synchronization often starts at the same time and is coordinated. However, different objects have a slightly different understanding of "synchronization". For example, device synchronization refers to the provision of a common time reference between two or more devices; data synchronization, operating data and database content are kept consistent; files Synchronization means that the data of the operation in the memory is consistent with the data in the file.

In writing code, there are some differences between the synchronization in communication and the synchronization in life. "Same" means collaboration, assistance, and cooperation. The main purpose is to coordinate synchronization and run in a predetermined sequence.

 

2. Thread synchronization

 Thread synchronization is simply the operation of one or more shared resources coordinated by multiple threads. Because the thread is executed asynchronously [execute your own, you can control where I execute it]. Therefore, when multiple threads operate on the same resource, confusion and resource competition will occur, which may cause resource data to be messed up. So use thread synchronization, but think of it as a coordinated sequential operation of this resource, rather than grabbing it. [ In fact, I changed a target to grab the mutex instead of data ]

 

3. Mutex (mutex) [ mutex is a recommended lock. Threads that are not locked or unlocked can still operate shared resources and become disordered ], so we have to follow a convention that is to operate after locking. Unlock when finished . Mutex mutex, Linux provides a mutex lock mutex (also called a mutex). Each thread tries to lock the resource before operating it. The lock can only be operated if the lock is successfully locked, and the operation is completed and unlocked. Resources are still shared, and threads are still competing, but through the "lock", resource access becomes a mutually exclusive operation, and then time-related errors will no longer occur. As shown


Main application functions:
pthread_ mutex. init function
pthread_ mutex_ destroy function
pthread_ mutex_ lock function
pthread_ mutex_ trylock function
pthread_ mutex_ unlock

function The return values ​​of the above five functions are: return 0 on success, return error number on failure. . The
pthread_mutex_t type is essentially a structure. In order to simplify the understanding, the implementation details can be ignored during application and simply treated as integers.
pthread_mutex_t mutex; variable mutex. There are only two values ​​1, 0.
 

Four. A detailed introduction to the mutex function

1. pthread_ mutex init function
initializes a mutex (mutex) --> the initial value can be regarded as 1
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
Parameter 1: Outgoing parameters, should be called when calling Pass &mutex;
restrict keyword: only used to restrict the pointer, telling the compiler that all operations that modify the pointer to the contents of the memory can only be done through this pointer.
Can not be modified by other variables or pointers other than this pointer.

Reference 2: Mutex attributes. It is an incoming parameter, usually NULL, select the default attribute (shared between threads). Refer to APUE.12.4 Synchronization Properties

  • 1. Static initialization: If the mutex lock mutex is statically allocated (defined globally, or modified by the static keyword), you can directly use the macro to initialize. pthread_mutex_t muetx = PTHREAD_MUTEX_INITIALIZER;
  • 2. Dynamic initialization: local variables should be dynamically initialized. pthread_ mutex init(&mutex, NULL)
     

2. The pthread_mutex_destroy function
destroys a mutex lock
int pthread_mutex_destroy(pthread_ mutex_ t *mutex);


3. pthread mutex lock function to
lock. It can be understood as mutex-- (or -1)
int pthread_ mutex_ lock(pthread_ mutex_t *mutex);


4. pthread_mutex_unlock function to
unlock. It can be understood as mutex++ (or +1)
int pthread_ mutex _unlock(pthread_ mutex _t * mutex);
 

5. pthread_mutex_trylock function
Try to lock
int pthread_mutex_trylock(pthread_mutex_t * mutex);
this pthread_mutex_trylock means to lock, if it can’t be added, even [ non-blocking lock ] will go to do other things. So this function should be called in a round-robin way

 

Guess you like

Origin blog.csdn.net/qq_44065088/article/details/109186498