Threads use mutexes to achieve mutual exclusion between threads, the mutex itself is a state of identification of resources, when it can be applied to the lock when the resources can be used, when the lock failed to request that the resource is occupied by other threads at this time is not available, we can use the semaphore to replace the mutex implementation. Semaphores are used to indicate the number of resources, and when a thread is going to access the resource, the semaphore must be applied first, and if the semaphore can be obtained then the thread can run normally, and if the resource fails then it needs to wait. When a thread acquires the semaphore and completes the execution, it must release the corresponding semaphore to notify other threads to work, that is, the use of semaphores can not only achieve the mutual exclusion of threads, but also can achieve synchronization between threads.
semaphore operation:① initializes int sem_init (sem_t *sem, int pshared, unsigned int value); Parameter description: SEM: Used to identify that semaphore, is a sem_t type with a value of pshared:pshared 0, indicating that the semaphore acts on the synchronization value between threads under the same process: the initial value return value of Semaphore: successful return 0, failure return error code ② destroy semaphore int SEM _destroy (sem_t *sem); Parameter description: SEM: Used to identify which semaphore return value, successfully return 0, failure return error code ③ obtain resource operation (p operation) int sem_wait (sem_t *sem); Function Description: Wait operation, so that the value of the semaphore minus 1, if it is already 0, then the thread is suspended to the waiting queue. Parameter description: SEM: Used to identify what the semaphore return value: Successfully returned 0, failure returned error code int sem_trywait (sem_t *sem); The function works like the function above, but this function does not suspend the thread for waiting. ④ Free resource operation (v operation) int sem_post (sem_t *sem); Function Description: Psot operation, so that the semaphore value plus 1, while waking the thread waiting for the semaphore. Parameter description: SEM: identification is which semaphore return value: Successfully returned 0, failure return error code
Example:The ring queue is set to a buffer, and the producer puts a data into the buffer each time it is produced, and the consumer reads a data from the buffer. At this point, you need to use two semaphores, one to indicate the number of hollow positions in the ring queue, and one to represent the number of data in the ring queue.
Producers can produce data only when they have a vacant position, and consumers can only get data when they have data
#include <stdio.h> #include <semaphore.h> #include <pthread.h>//array analog ring queue int Arr[8] = {0};
int i = 0;
int j = 0;
Sets the semaphore to represent an empty sem_t blank in an array;
Set the semaphore to represent the data in the array sem_t;
void *product (void *arg) {while (1) {//producer must have an empty seat, otherwise it can only wait for the consumer to take the data before producing the sem_wait (&blank);
ARR[I%8] = rand ()%123;
printf ("Producer Production Complete:%d\n", arr[i%8]);
i++;
When the production is successful, the amount of data is +1 sem_post (&data);
Sleep (i%3);
} void *consumer (void *arg) {while (1) {//consumer must have data, or wait for producers to produce data before taking sem_wait (&data);
printf ("Consumer consumption Complete:%d\n", arr[j%8]);
j + +;
When the consumption is successful, the vacancy amount is +1 sem_post (&blank);
Sleep (j%5);
int main () {///At the beginning, there are 8 empty sem_init (&blank, 0, 8);
At first, no data was put into the data, with 0 sem_init (&data, 0, 0);
pthread_t Thread1;
pthread_t thread2;
Pthread_create (&thread1, NULL, &product, NULL);
Pthread_create (&thread2, NULL, &consumer, NULL);
Pthread_join (Thread1, NULL); PthRead_join (Thread2, NULL);
Sem_destroy (&blank);
Sem_destroy (&data);
return 0;
}