}
The running result is as follows:
In fact, the printer is used for processing. When printing, others are not allowed to print the printer. Only when I print the printer, others are allowed to print it. This process is a bit similar. Put the printer in a room and lock the room. This lock is opened by default. When A needs to print the lock, he first checks whether the lock is locked. If not, he goes in and prints the lock in the room. At this moment, B also needs to print, B also checks the lock first, and finds that the lock is locked, and he will wait outside the door. After printing A, B locks out the lock.
There is also a lock in the thread-mutex. mutex is a simple lock method to control access to shared resources. There are only two States of mutex, locks and unlocks ).
The procedure of the mutex lock is as follows:
1) Lock the mutex lock before accessing the critical area after shared resources.
2) release the lock on the mutex lock export after the access is completed.
3) after the mutex lock is applied, any other thread that tries to lock the mutex lock again will be blocked until the lock is released.
The mutex lock data type is:Pthread_mutex_t.
Mutex lock operationHeader files required by the following functions:
# Include
1) initialize the mutex lock
Int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr );
Function:
Initializes a mutex lock.
Parameters:
Mutex: Mutex lock address. The type is pthread_mutex_t.
Attr: Sets the mutex attribute. Generally, the default attribute can be used to set attr to NULL.
You can use the macro PTHREAD_MUTEX_INITIALIZER to statically initialize mutex locks, for example:
Pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
This method is equivalent to calling pthread_mutex_init () with the attr parameter specified by NULL to perform dynamic initialization. The difference is that the PTHREAD_MUTEX_INITIALIZER macro does not perform error checks.
Return Value:
Successful: 0. The applied lock is opened by default.
Failed: Non-0 error code
2) Lock
Int pthread_mutex_lock (pthread_mutex_t * mutex );
Function:
Lock a mutex lock. If the mutex lock is locked, the caller will block it until the lock is unlocked.
Parameters:
Mutex: Mutex lock address.
Return Value:
Success: 0
Failed: Non-0 error code
Int pthread_mutex_trylock (pthread_mutex_t * mutex );
When this function is called, if the mutex lock is not locked, the lock is locked and 0 is returned. If the mutex lock has been locked, the function returns failure, that is, EBUSY.
3) Unlock
Int pthread_mutex_unlock (pthread_mutex_t * mutex );
Function:
Unlock the specified mutex lock.
Parameters:
Mutex: Mutex lock address.
Return Value:
Success: 0
Failed: Non-0 error code
4) destroy the mutex lock
Int pthread_mutex_destroy (pthread_mutex_t * mutex );
Function:
Destroys a mutex. After the mutex lock is used, the mutex lock must be destroyed to release resources.
Parameters:
Mutex: Mutex lock address.
Return Value:
Success: 0
Failed: Non-0 error code
Mutex lock application instanceWe use the mutex lock to complete the above example. The sample code is as follows:
[Cpp]View plaincopy
- # Include
- # Include
- # Include
-
- Pthread_mutex_t mutex; // mutex lock
-
- // Printer
- Void printer (char * str)
- {
- Pthread_mutex_lock (& mutex); // lock
- While (* str! = '\ 0 ')
- {
- Putchar (* str );
- Fflush (stdout );
- Str ++;
- Sleep (1 );
- }
- Printf ("\ n ");
- Pthread_mutex_unlock (& mutex); // unlock
- }
-
- // Thread 1
- Void * thread_fun_1 (void * arg)
- {
- Char * str = "hello ";
- Printer (str); // print
- }
-
- // Thread 2
- Void * thread_fun_2 (void * arg)
- {
- Char * str = "world ";
- Printer (str); // print
- }
-
- Int main (void)
- {
- Pthread_t tid1, tid2;
-
- Pthread_mutex_init (& mutex, NULL); // initialize the mutex lock
-
- // Create two threads
- Pthread_create (& tid1, NULL, thread_fun_1, NULL );
- Pthread_create (& tid2, NULL, thread_fun_2, NULL );
-
- // Wait until the thread ends and reclaim its resources
- Pthread_join (tid1, NULL );
- Pthread_join (tid2, NULL );
-
- Pthread_mutex_destroy (& mutex); // destroy the mutex lock
-
- Return 0;
- }
The running result is as follows: