POSIX信号量
一、信號量
進(jìn)化版的互斥鎖(1 N)
由于互斥鎖的粒度比較大,如果我們希望在多個線程間對某一對象的部分?jǐn)?shù)據(jù)進(jìn)行共享,使用互斥鎖是沒有辦法實現(xiàn)的,只能將整個數(shù)據(jù)對象鎖住,這樣雖然達(dá)到了多線程操作共享數(shù)據(jù)時保證數(shù)據(jù)正確性的目的,卻無形中導(dǎo)致線程的并發(fā)性下降,線程從并行執(zhí)行,變成了串行執(zhí)行,與直接使用單進(jìn)程無異。
?
二、主要應(yīng)用函數(shù)
1 函數(shù)原型:初始化信號量
sem_init(sem_t *sem, int pshared, unsigned int value); sem_destroy(sem_t *sem); //銷毀信號量分析:
- pshared:pshared參數(shù)表明是否在多個進(jìn)程中使用信號量。(0 - 線程同步;1 - 進(jìn)程同步)
- value:最多有幾個線程操作共享數(shù)據(jù)。
?
2. 函數(shù)原型:加鎖。調(diào)用一次相當(dāng)于對sem做了-- 操作;如果sem值為0, 線程會阻塞
sem_wait(sem_t *sem); // 加鎖。調(diào)用一次相當(dāng)于對sem做了-- 操作;如果sem值為0, 線程會阻塞 sem_trywait(sem_t *sem); // 嘗試加鎖。sem == 0, 加鎖失敗, 不阻塞, 直接返回 sem_post(sem_t *sem); // 解鎖 ++。對sem做了++操作?
三、生產(chǎn)者消費(fèi)者信號量模型
測試代碼:
/*信號量實現(xiàn)生產(chǎn)者 消費(fèi)者問題*/#include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <stdio.h> #include <semaphore.h>#define NUM 5int queue[NUM]; sem_t blank_number, product_number; //全局?jǐn)?shù)組實現(xiàn)環(huán)形隊列//空格子信號量、產(chǎn)品信號量void *producer(void *arg) {int i = 0;while (1){sem_wait(&blank_number); //產(chǎn)者將空格子--,為0則則塞等待queue[i] = rand() % 1000 + 1; //生產(chǎn)一個產(chǎn)品printf("-----Produce-----%d\n", queue[i]);sem_post(&product_number); //將產(chǎn)品數(shù)++i = (i + 1) % NUM; //借助小標(biāo)實現(xiàn)環(huán)形隊列sleep(rand() % 3);} }void *consumer(void *arg) {int i = 0;while (1){sem_wait(&product_number); //消費(fèi)者將產(chǎn)品--。為0則阻塞等待printf("---Consumer-----%d\n", queue[i]);queue[i] = 0; //消費(fèi)掉一個產(chǎn)品sem_post(&blank_number); //消費(fèi)掉產(chǎn)品,將空格子++i = (i + 1) % NUM;sleep(rand() % 3);} }int main(int argc, char *arg[]) {pthread_t pid, cid;sem_init(&blank_number, 0, NUM); //初始化空格子信號量為5sem_init(&product_number, 0, 0); //產(chǎn)品數(shù)為0pthread_create(&pid, NULL, producer, NULL);pthread_create(&cid, NULL, consumer, NULL);pthread_join(pid, NULL);pthread_join(cid, NULL);sem_destroy(&blank_number);sem_destroy(&product_number);return 0; }輸出結(jié)果:
?
總結(jié)
- 上一篇: 关西无极刀剧情介绍
- 下一篇: 存储映射I/O(一)