【POSIX标准头文件】<semaphore.h>

函数列表

函数说明
sem_init初始化信号量
sem_wait等待信号量大于0
sem_post信号量+1
sem_getvalue获取信号量的值到参数2
sem_destroy销毁信号量,只有初始化之后的才能销毁

生产者消费者模型

//信号量
sem_t sem;

class Phone {
private:
    int phoneId;
public:
    explicit Phone(int id) : phoneId(id) {

    }

    int getPhoneId() {
        return phoneId;
    }
};

int productPhoneId;
std::list<Phone> phoneList;

void *phone_producer_runnable(void *params) {
    while (true) {
        sleep(random() % 10);
        Phone phone(productPhoneId++);
        phoneList.push_back(phone);

        //信号量+1
        sem_post(&sem);
    }
    return nullptr;
}

void *phone_consumer_runnable(void *params) {
    while (true) {
        //等待信号量大于0
        sem_wait(&sem);

        Phone phone = phoneList.front();
        phoneList.pop_front();
    }

    return nullptr;
}

void testProducerConsumer2() {
    //参数1:指向的信号对象
    //参数2:控制信号量的类型,如果其值为0,就表示信号量是当前进程的局部信号量,否则信号量就可以在多个进程间共享
    //参数3:信号量初始值
    sem_init(&sem, 0, 0);

    pthread_t thread1;
    pthread_create(&thread1, nullptr, phone_producer_runnable, nullptr);

    pthread_t thread2;
    pthread_create(&thread2, nullptr, phone_consumer_runnable, nullptr);
}

信号量和条件变量区别

  • 使用条件变量可以一次唤醒所有等待者,而信号量不能唤醒
  • 条件变量需要在等待前持有锁,而信号量不需要
  • 信号量始终有一个值(状态的),而条件变量是没有
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include <pthread.h> sem_t apple; sem_t orange; sem_t empty; pthread_mutex_t mutex; void *Dad() { int nextp = 0; int i = 0; for(i = 0; i < 10; ++i) { int time = rand() % 10 + 1; //随机使程序睡眠0点几秒 usleep(time*100000); sem_wait(&empty); pthread_mutex_lock(&mutex); if(nextp == 0) { printf("爸爸放入了一个苹果\n"); } else { printf("爸爸放入了一个桔子\n"); } fflush(stdout); pthread_mutex_unlock(&mutex); //互斥锁解锁 if(nextp == 0) { sem_post(&apple); } else { sem_post(&orange); } nextp = 1 - nextp; } } void *Daughter() { while(1) { int time = rand() % 10 + 1; //随机使程序睡眠0点几秒 usleep(time * 100000); sem_wait(&apple); pthread_mutex_lock(&mutex); printf("女儿取了一个苹果\n") ; fflush(stdout); pthread_mutex_unlock(&mutex); //互斥锁解锁 sem_post(&empty); } } void *Son() { while(1) { int time=rand()%10+1; usleep(time*10000); sem_wait(&orange); pthread_mutex_lock(&mutex); printf("儿子取了一个桔子\n") ; fflush(stdout); pthread_mutex_unlock(&mutex); sem_post(&empty); } //请添加儿子线程的函数代码 } int main() { sem_init(&empty, 0, 5); //信号量初始化 sem_init(&orange, 0, 0); sem_init(&apple, 0, 0); pthread_mutex_init(&mutex, NULL); //互斥锁初始化 pthread_t dadid; pthread_t daughterid; pthread_t sonid; pthread_create(&dadid, NULL, Dad, NULL); //创建爸爸线程 pthread_create(&daughterid, NULL, Daughter, NULL); //创建女儿线程 pthread_create(&sonid, NULL, Son, NULL); //创建儿子线程 pthread_join(daughterid, NULL); pthread_join(sonid, NULL); sem_destroy(&empty); //信号量的销毁 sem_destroy(&apple); sem_destroy(&orange); pthread_mutex_destroy(&mutex); //互斥锁的销毁 return 0; } 帮我注释每一条代码
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值