4线程同步:信号量
1信號(hào)量
信號(hào)量可以有n把鎖。
依賴(lài)的頭文件
#include <semaphore.h>
函數(shù)聲明
sem_t表示信號(hào)量
?
int sem_init(sem_t *sem, int pshared,unsigned int value);
| 名稱(chēng): | sem_init |
| 功能: | initialize an unnamed semaphore,初始化信號(hào)量sem_t,初始化的時(shí)候可以指定信號(hào)量的初始值,以及是否可以在多進(jìn)程間共享。 |
| 頭文件: | #include <semaphore.h> |
| 函數(shù)原形: | int sem_init(sem_t *sem, int pshared, unsigned int value); |
| 參數(shù): | ? |
| 返回值: | sem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error. |
?
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
| 名稱(chēng): | sem_wait sem_trywait |
| 功能: | lock a semaphore?一直阻塞等待直到信號(hào)量 > 0. |
| 頭文件: | #include <semaphore.h> |
| 函數(shù)原形: | int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); |
| 參數(shù): | ? |
| 返回值: | All of these functions return 0 on success; on error, the value of?the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error. |
?
int sem_timedwait(sem_t *sem, const structtimespec *abs_timeout);
| 名稱(chēng): | sem_timedwait |
| 功能: | lock a semaphore,阻塞等待若干時(shí)間直到信號(hào)量 > 0 |
| 頭文件: | #include <semaphore.h> |
| 函數(shù)原形: | int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); |
| 參數(shù): | ? |
| 返回值: | All of these functions return 0 on success; on error, the value of?the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error. |
?
int sem_post(sem_t *sem);
| 名稱(chēng): | sem_post |
| 功能: | unlock a semaphore,使信號(hào)量加1。 |
| 頭文件: | #include <semaphore.h> |
| 函數(shù)原形: | int sem_post(sem_t *sem); |
| 參數(shù): | ? |
| 返回值: | sem_post() returns 0 on success; on error, the value of the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error. |
?
int sem_destroy(sem_t *sem);
| 名稱(chēng): | sem_destroy |
| 功能: | destroy an unnamed semaphore釋放信號(hào)量。和sem_init對(duì)應(yīng) |
| 頭文件: | #include <semaphore.h> |
| 函數(shù)原形: | int sem_destroy(sem_t *sem); |
| 參數(shù): | ? |
| 返回值: | sem_destroy() return 0 on success;on error,-1 is returned,an errno is set to indicate the error. |
案例說(shuō)明:
| #include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <semaphore.h> ? #define NUM 5 ? int queue[NUM]; sem_t blank_number,product_number; ? void *producer(void *arg) { ???int p = 0; ???while(1) { ???????//blank_num = 5生產(chǎn)者最多生產(chǎn)5個(gè) ???????//一直阻塞等待信號(hào)量大于0 ???????sem_wait(&blank_number); ???????queue[p] = rand() % 1000 + 1; ???????printf("Produce %d\n",queue[p]); ???????//product_number = 0 ->1 ???????//使信號(hào)量加1 ???????sem_post(&product_number); ???????p = (p + 1) % NUM; ???????sleep(rand() % 5); ???} } ? void *consumer(void *arg) { ???int c = 0; ???while (1) { ???????//等待信號(hào)量大于0 ???????sem_wait(&product_number); ???????printf("Consume %d\n",queue[c]); ???????queue[c] = 0; ???????//使信號(hào)量加1 ???????sem_post(&blank_number); ???????c = (c + 1) % NUM; ???????sleep(rand() % 5); ???} } int main(int argc ,char *argv[]) { ???pthread_t pid,cid; ???//將blank_num信號(hào)量初始化的值為5 ???sem_init(&blank_number,0,NUM); ???//將product_number信號(hào)量初始化的值變?yōu)? ???sem_init(&product_number,0,0); ???pthread_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; } |
運(yùn)行結(jié)果:
總結(jié)
- 上一篇: 3线程同步:条件变量
- 下一篇: 5进程间锁:进程间pthread_mut