[Linux]多线程同步之sem_wait()学习笔记

转载:https://blog.csdn.net/tzshlyt/article/details/53150697 

1、semaphore 的这种信号量不仅可用于同一进程的线程同步,也可以用于不同进程间同步。

一个生产者-消费者例子:生产者不停的向一个固定大小的环形队列中添加数据,消费者从环形队列中清零数据,如果生产者积累的数据大于环形队列长度,则等待消费者清除数据有空位后再生产。

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <unistd.h>
 
#define NUM 5
int queue[NUM];
 
sem_t blank_number, product_number;
 
void *producer(void *arg)
{
    int p = 0;
    while(1){
        sem_wait(&blank_number);
        queue[p] = rand() % 1000 + 1;
        printf("produce %d\n", queue[p]);
        p = (p + 1) % NUM;
        sem_post(&product_number);
        sleep(1);
        //sleep(rand()%5);
    }    
}
 
void *consumer(void *arg)
{
    int c = 0, i;
    while(1) {
        sem_wait(&product_number);
        for(i=0; i < NUM; i++) {
            printf("%d ", queue[i]);
        }
        putchar('\n');
 
        printf("consume %d\n", queue[c]);
        queue[c] = 0;
        sem_post(&blank_number);
        c = (c+1)%NUM;   
        sleep(3);
        //sleep(rand()%5);
    }
}
 
int main()
{
    pthread_t pid, cid;
    
    // 可用资源数为 NUM = 5
    sem_init(&blank_number, 0, NUM);
    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;
}

运行结果:

2、用Condition Variable实现Semaphore ,即用pthread_cond_wait()等方式实现sem_wait()等方式。

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
 
#define NUM 5
int queue[NUM];
int current_num; 
 
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_cond_t blank_product = PTHREAD_COND_INITIALIZER;
 
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 
void *producer(void *arg)
{
    int p = 0, i;
    while(1){
        
        pthread_mutex_lock(&lock);
        while(current_num >= NUM) {
            pthread_cond_wait(&blank_product, &lock);
        }
 
        queue[p] = rand() % 1000 + 1;
        printf("produce %d\n", queue[p]);
        p = (p + 1) % NUM;
        current_num ++;
        pthread_mutex_unlock(&lock);
        
        printf("after produce --- current_num: %d\n", current_num);
        for(i=0; i < NUM; i++) {
            printf("%d ", queue[i]);
        }
        putchar('\n');
 
        sleep(1);
       //sleep(rand()%5);
       
        pthread_cond_signal(&has_product);       
    }    
}
 
void *consumer(void *arg)
{
    int c = 0, i;
    while(1) {
       
        
        pthread_mutex_lock(&lock);
        pthread_cond_wait(&has_product, &lock);
        printf("consume %d\n", queue[c]);
        
        queue[c] = 0;
        c = (c+1)%NUM;   
        current_num--;
        printf("after consume --- current_num: %d\n", current_num);
        for(i=0; i < NUM; i++) {
            printf("%d ", queue[i]);
        }
        putchar('\n');
    
        pthread_mutex_unlock(&lock);
       
        sleep(3);
        //sleep(rand()%5);
        
        pthread_cond_signal(&blank_product);
    }
}
 
int main()
{
    pthread_t pid, cid;
 
    pthread_create(&pid, NULL, producer, NULL);
    pthread_create(&cid, NULL, consumer, NULL);
    pthread_join(pid, NULL);
    pthread_join(cid, NULL);
    printf("----\n");
    return 0;
}

 运行结果:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值