Thread synchronization-semaphore (semaphore)
Rethinking the problems of producers and consumers
In real life, as long as there are goods, consumers can consume, this is no problem. But the production of producers is not unlimited, for example, the warehouse is limited, the raw materials are limited, production indicators are limited by consumption indicators and so on. In order to further solve the problem of producers and consumers, the introduction of signal volume into the mechanism.
Signal Volume
The Semaphore (semaphore) is an upgraded version of the mutex: the state of the mutex is 0 or 1, and the semaphore can be n. That is, when a mutex is used, a maximum of one thread is allowed to enter the critical area, and the semaphore allows multiple, and the actual value is the current internal value of the semaphore.
related Functions
sem_t //Semaphore type Sem_init (sem_t *sem, int pshared, unsigned int value); sem_wait (sem_t *sem) sem_trywaitsem_ Timedwaitsem_post (sem_t *sem) Sem_destroy
It is important to understand: sem_wait and sem_post two functions.
Sem_wait (SEM); When the SEM is zero, the thread blocks; otherwise, the SEM minus one, the thread does not block.
Sem_post (SEM), SEM plus one.
Also, using the Sem_init method, for semaphore types, initialization, the second parameter, the default is 0, which is indicated for use between threads. The third parameter specifies the initial value.
single producer and single consumer
#include <stdio.h> #include <unistd.h > #include <pthread.h> #include <semaphore.h> #define NUM 5sem_t blank_num, Product_num;int I, J, K;int Goods[num];void *producer (void *argv) {while (1) {sem_wait (&blank_num); Goods[i] = rand ()% + 1;printf ("Produce%d\ N ", Goods[i]); Sem_post (&product_num); i = (i + 1)% Num;sleep (rand ()% 2);}} void *comsumer (void *argv) {while (1) {sem_wait (&product_num);p rintf ("Comsume%d\n", Goods[j]); Goods[j] = 0;sem_ Post (&blank_num); j = (j + 1)% Num;sleep (rand ()% 2);}} int main (void) {i = j = k = 0;//Initialize semaphore sem_init (&blank_num, 0, num); Sem_init (&product_num, 0, 0);p thread_t Pro, com; Pthread_create (&com, NULL, producer, NULL);p thread_create (&pro, NULL, Comsumer, NULL);p thread_join (COM, null) ;p thread_join (Pro, NULL); Sem_destroy (&blank_num); Sem_destroy (&product_num); return 0;}
Multi-producer and multi-consumer
#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #define NUM 5pthread_mutex_t M1, m2;sem_t blank_num, Product_num;int goods[num];int i, J, k;void *producer (void *argv) {while (1) {Sem_ Wait (&blank_num);p thread_mutex_lock (&M1); Goods[i] = rand ()% + 1;printf ("Produce%d\n", goods[i]); i = (i + 1) % Num;pthread_mutex_unlock (&M1); Sem_post (&product_num); Sleep (rand ()% 2);}} void *comsumer (void *argv) {while (1) {sem_wait (&product_num);p thread_mutex_lock (&m2);p rintf ("Comsume%d\n", GOODS[J]); Goods[j] = 0; 0 J = (j + 1)% Num;pthread_mutex_unlock (&m2); Sem_post (&blank_num); Sleep (rand ()% 2);}} int main (void) {i = j = k = 0;//Initialize semaphore and mutex Sem_init (&blank_num, 0, num); Sem_init (&product_num, 0, 0);p thread_mutex_ Init (&M1, NULL);p thread_mutex_init (&m2, null);p thread_t pro[2], com[3];for (k = 0; k < 3; k++) Pthread_create (& AMP;COM[K], NULL, producer, null); for (k = 0; k < 2; k++) Pthread_create (&p Ro[k], NULL, comsumer, NULL); for (k = 0; k < 3; k++) Pthread_join (com[k], NULL); for (k = 0; k < 2; k++) Pthread_join (Pro[k], NULL);p Thread_mutex_destroy (&M1);p Thread_mutex_destroy (&m2); Sem_destroy (&blank_num); sem_ Destroy (&product_num); return 0;}
CCPP Blog Directory
Copyright NOTICE: This article is for bloggers original articles, reproduced, please indicate the source.
Linux system Programming: Thread Synchronization-semaphore (semaphore)