您的位置:首页 > 运维架构 > Linux

线程同步--信号量

2016-12-20 16:52 302 查看
线程同步方法 信号量不常用,找到个帖子不错,记录一下!

依赖的头文件

#include <semaphore.h>

函数声明

sem_t表示信号量

 

int sem_init(sem_t *sem, int pshared,unsigned int value);

名称:
sem_init
功能:
initialize an unnamed semaphore,初始化信号量sem_t,初始化的时候可以指定信号量的初始值,以及是否可以在多进程间共享。
头文件:
#include <semaphore.h>
函数原形:
int sem_init(sem_t *sem, int pshared, unsigned int value);
参数:
 
返回值:
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);

名称:
sem_wait

sem_trywait
功能:
lock a semaphore 一直阻塞等待直到信号量
> 0.
头文件:
#include <semaphore.h>
函数原形:
int sem_wait(sem_t *sem);

int sem_trywait(sem_t *sem);
参数:
 
返回值:
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);

名称:
sem_timedwait
功能:
lock a semaphore,阻塞等待若干时间直到信号量 > 0
头文件:
#include <semaphore.h>
函数原形:
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
参数:
 
返回值:
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);

名称:
sem_post
功能:
unlock a semaphore,使信号量加1。
头文件:
#include <semaphore.h>
函数原形:
int sem_post(sem_t *sem);
参数:
 
返回值:
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);

名称:
sem_destroy
功能:
destroy an unnamed semaphore释放信号量。和sem_init对应
头文件:
#include <semaphore.h>
函数原形:
int sem_destroy(sem_t *sem);
参数:
 
返回值:
sem_destroy() return 0 on success;on error,-1 is returned,an errno is set to indicate the error.
案例说明:

#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生产者最多生产5个

       //一直阻塞等待信号量大于0

       sem_wait(&blank_number);

       queue[p] = rand() % 1000 + 1;

       printf("Produce %d\n",queue[p]);

       //product_number = 0 ->1

       //使信号量加1

       sem_post(&product_number);

       p = (p + 1) % NUM;

       sleep(rand() % 5);

   }

}

 

void *consumer(void *arg) {

   int c = 0;

   while (1) {

       //等待信号量大于0

       sem_wait(&product_number);

       printf("Consume %d\n",queue[c]);

       queue[c] = 0;

       //使信号量加1

       sem_post(&blank_number);

       c = (c + 1) % NUM;

       sleep(rand() % 5);

   }

}

int main(int argc ,char *argv[])

{

   pthread_t pid,cid;

   //将blank_num信号量初始化的值为5

   sem_init(&blank_number,0,NUM);

   //将product_number信号量初始化的值变为0

   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;

}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息