When we need to control access to shared resources, we can use a simple lock-up method to control. We can create a read / write program that shares a shared buffer and uses a mutex to control access to the buffer.
The function pthread_mutex_init () is used to generate a mutex lock. Its function prototype is as follows:
#include <pthread.h>
int Pthread_mutex_init (pthread_mutex_t *restrict Mutex,const pthread_mutexattr_t *restrict attr) ;
The first parameter is the address of the mutex, and the second parameter sets the property of the mutex variable, in most cases . Select default property, the null pointer is passed in NULL.
The Pthread_mutex_lock () function declaration begins with a mutex lock, and thereafter the code is locked until it calls Pthread_mutex_ unlock (), i.e. only one thread can invoke execution at a time. When a thread executes to pthread_mutex_lock (), if the lock is being used by another thread at this point, then the thread is blocked and thread is blocked until the other thread is released. These two function prototypes are:
int Pthread_mutex_lock (pthread_mutex_t *mutex);
int Pthread_mutex_unlock (pthread_mutex_t *mutex);
the parameters of the two functions are the addresses of the mutex variables. The function execution successfully returns 0, otherwise the error number is returned.
Mutexes are easy to use, but one drawback is that there is a good chance of deadlocks in the process of using mutexes: Two threads attempting to occupy two resources at the same time and locking the corresponding mutex in different order, for example, two threads need to be lockedMutual exclusion LockLand mutual exclusion locks2,AThe thread locks the mutexL,Bthread locks the mutex2, this time ifAthread in the solutionexcept for mutual exclusion lockLlocked the mutex before.2, whileByou just need to lock the mutex again.L, there is a deadlock. It looks like a tongue twister, which is a bit of a popular explanation: threadingAand ThreadsBall require exclusive use2a resource, but heWe all occupy one resource first, and then we wait for the release of another resource, which forms a deadlock.at this point we can use the functionPthread_mutex_trylock (), it is a functionPthread_mutex_lock ()the non-blocking function, when it finds that the deadlock is unavoidable, it passesErrnoreturnEbusy, we can target the deadlockmake the appropriate treatment. Pthread_mutex_try_lock ()the function prototypes are:
int Pthread_mutex _trylock (Pthread_mutex_t*mutex);
when the mutex is no longer in use, the mutex should be released, and the function pthread _mutex.destory () is used to release a mutex variable.
Code:
#include <stdio.h>#include<pthread.h>#include<unistd.h>Charbuffer[ -];intBuffer_has_data=0;p thread_mutex_t mutexes;voidWrite_buffer (Char*data) { /*Lock Mutex*/Pthread_mutex_lock (&mutex); if(Buffer_has_data = =0) {sprintf (buffer,"%s", data); Buffer_has_data=1; } /*Open the mutex lock*/Pthread_mutex_unlock (&mutex);}voidRead_buffer (void){ while(1){ /*Lock Mutex*/Pthread_mutex_lock (&mutex); if(Buffer_has_data = =1) {printf ("Read buffer, data = [%s]\n", buffer); Buffer_has_data=0; } /*Open the mutex lock*/Pthread_mutex_unlock (&mutex); Sleep (1); }}intMain (intargcChar**argv) { Charinput[ -]; pthread_t reader; /*Initializes a mutex object with default properties*/Pthread_mutex_init (&mutex, NULL); Pthread_create (&reader, NULL, (void*) (Read_buffer), NULL); while(1) {scanf ("%s", input); Write_buffer (input); } return 0;}
Linux thread Programming 2.0--thread synchronization-Mutex lock