码迷,mamicode.com
首页 > 编程语言 > 详细

4线程同步:信号量

时间:2014-09-25 19:49:37      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   文件   

1 信号量

信号量可以有n把锁。

依赖的头文件

#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_int对应

头文件:

#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信号量初始化的值变为5

    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;

}

运行结果:

bubuko.com,布布扣



4线程同步:信号量

标签:des   style   blog   http   color   io   os   ar   文件   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/39554075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!