第31章 信号量

31.1 信号量的定义

信号量是有一个整数值的对象,可以用两个函数来操作它。在 POSIX 标准中,是sem_wait()和 sem_post()1。

#include <semaphore.h>
sem_t s;
sem_init(&s, 0, 1);

其中申明了一个信号量 s,通过第三个参数,将它的值初始化为 1。sem_init()的第二个参数,在我们看到的所有例子中都设置为 0,表示信号量是在同一进程的多个线程共享的。

31.2 二值信号量(锁)

信号量的第一种用法是我们已经熟悉的:用信号量作为锁。在图 31.3 所示的代码片段里,我们直接把临界区用一对 sem_wait()/sem_post()环绕。

sem_t m;
sem_init(&m, 0, X); // initialize semaphore to X; what should X be?
sem_wait(&m);
// critical section here
sem_post(&m);

31.3 信号量用作条件变量

信号量也可以用在一个线程暂停执行,等待某一条件成立的场景。例如,一个线程要
等待一个链表非空,然后才能删除一个元素。在这种场景下,通常一个线程等待条件成立,
另外一个线程修改条件并发信号给等待线程,从而唤醒等待线程。因为等待线程在等待某
些条件(condition)发生变化,所以我们将信号量作为条件变量

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#include "common.h"
#include "common_threads.h"

#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif

sem_t s;

void *child(void *arg) {
    sleep(2);
    printf("child\n");
    Sem_post(&s); // signal here: child is done
    return NULL;
}

int main(int argc, char *argv[]) {
    Sem_init(&s, 0); 
    printf("parent: begin\n");
    pthread_t c;
    Pthread_create(&c, NULL, child, NULL);
    Sem_wait(&s); // wait here for child
    printf("parent: end\n");
    return 0;
}

31.4 生产者/消费者(有界缓冲区)问题

31.4 读者—写者锁

另一个经典问题源于对更加灵活的锁定原语的渴望,它承认不同的数据结构访问可能
需要不同类型的锁。例如,一个并发链表有很多插入和查找操作。插入操作会修改链表的
状态(因此传统的临界区有用) ,而查找操作只是读取该结构,只要没有进行插入操作,我们可以并发的执行多个查找操作。读者—写者锁(reader-writer lock)就是用来完成这种操作的

31.6 哲学家就餐问题

31.7 如何实现信号量

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#include "common.h"
#include "common_threads.h"
#include "zemaphore.h"

Zem_t s;

void *child(void *arg) {
    sleep(4);
    printf("child\n");
    Zem_post(&s); // signal here: child is done
    return NULL;
}

int main(int argc, char *argv[]) {
    Zem_init(&s, 0); 
    printf("parent: begin\n");
    pthread_t c;
    Pthread_create(&c, NULL, child, NULL);
    Zem_wait(&s); // wait here for child
    printf("parent: end\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值