Mutex refers to mutually exclusive locks. It is a semaphores that are often used to prevent two processes or threads from accessing the same shared resources at the same time.
1. Data Type:
In Linux, the thread mutex data type is pthread_mutex_t. We define a mutex data as follows:
Pthread_mutex_t mutex;
2. Function Description:
Header file: pthread. h
(1). mutex lock initialization:
Function prototype: int pthread_mutex_init (pthread_mutex_t * mutex,
Const pthread_mutexattr_t * mutexattr );
Function input value: mutex.
Mutexattr: pthread_mutex_initializer: Creates a fast mutex lock.
Pthread_recursive_mutex_initializer_np: Creates a recursive mutex lock.
Pthread_errorcheck_mutex_initializer_np: creates an error mutex lock.
Function return value: Success: 0
Error:-1
(2). mutex operation function
Int pthread_mutex_lock (pthread_mutex_t * mutex); // lock
Int pthread_mutex_trylock (pthread_mutex_t * mutex); // The lock is blocked only when the mutex is locked.
Int pthread_mutex_unlock (pthread_mutex_t * mutex); // unlock
Int pthread_mutex_destroy (pthread_mutex_t * mutex); // clear the mutex lock
Function input value: mutex.
Function return value: Success: 0
Error:-1
3. Usage:
Struct mutex;
Mutex_init (& mutex);/* definition */
...
Mutex_lock (& mutex);/* Get mutex lock */
.../* Critical resource */
Mutex_unlock (& mutex);/* release mutex lock */
Finally, let's start an exercise: We create two threads, access the global variable gnum, modify it, and print it out.
[CPP]View plaincopy
- /* Mutex. C */
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <pthread. h>
- # Include <errno. h>
- /* Global variable */
- Int gnum = 0;
- /* Mutex */
- Pthread_mutex_t mutex;
- /* Declare the thread to run the Service Program */
- Static void pthread_func_1 (void );
- Static void pthread_func_2 (void );
- Int main (void)
- {
- /* Thread identifier */
- Pthread_t pt_1 = 0;
- Pthread_t pt_2 = 0;
- Int ret = 0;
- /* Mutex initialization */
- Pthread_mutex_init (& mutex, null );
- /* Create thread 1 and thread 2 respectively */
- Ret = pthread_create (& pt_1, // thread ID pointer
- Null, // default attribute
- (Void *) pthread_func_1, // run the Function
- Null); // No Parameter
- If (Ret! = 0)
- {
- Perror ("pthread_cmdcreate ");
- }
- Ret = pthread_create (& pt_2, // thread identifier pointer
- Null, // default attribute
- (Void *) pthread_func_2, // run the Function
- Null); // No Parameter
- If (Ret! = 0)
- {
- Perror ("pthread_2_create ");
- }
- /* Wait for the end of thread 1 and thread 2 */
- Pthread_join (pt_1, null );
- Pthread_join (pt_2, null );
- Printf ("main programme exit! /N ");
- Return 0;
- }
- /* Service program of thread 1 */
- Static void pthread_func_1 (void)
- {
- Int I = 0;
- For (;;)
- {
- Printf ("this is pthread1! /N ");
- Pthread_mutex_lock (& mutex);/* obtain the mutex lock */
- /* Note: This is to prevent the thread from being preemptible, so that a thread can access mutex resources multiple times during another thread sleep. Therefore, sleep must be called after obtaining the mutex lock */
- Sleep (1 );
- /* Critical resources */
- Gnum ++;
- Printf ("thread1 add one to num: % d/N", gnum );
- Pthread_mutex_unlock (& mutex);/* release mutex lock */
- }
- }
- /* Service program of thread 2 */
- Static void pthread_func_2 (void)
- {
- Int I = 0;
- For (;;)
- {
- Printf ("this is pthread2! /N ");
- Pthread_mutex_lock (& mutex);/* obtain the mutex lock */
- /* Note: This is to prevent the thread from being preemptible, so that a thread can access mutex resources multiple times during another thread sleep. Therefore, sleep must be called after obtaining the mutex lock */
- Sleep (1 );
- /* Critical resources */
- Gnum ++;
- Printf ("thread2 add one to num: % d/N", gnum );
- Pthread_mutex_unlock (& mutex);/* release mutex lock */
- }
- Pthread_exit (0 );
- }
Then compile and run the program. We can see that the thread 1 and 2 access the shared resources in a peaceful way.