栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

第31章 信号量

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第31章 信号量

31.1 信号量的定义

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

#include 
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 
#include 
#include 
#include 

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

#ifdef linux
#include 
#elif __APPLE__
#include "zemaphore.h"
#endif

sem_t s;

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

int main(int argc, char *argv[]) {
    Sem_init(&s, 0); 
    printf("parent: beginn");
    pthread_t c;
    Pthread_create(&c, NULL, child, NULL);
    Sem_wait(&s); // wait here for child
    printf("parent: endn");
    return 0;
}
31.4 生产者/消费者(有界缓冲区)问题 31.4 读者—写者锁

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

31.6 哲学家就餐问题 31.7 如何实现信号量
#include 
#include 
#include 
#include 

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

Zem_t s;

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

int main(int argc, char *argv[]) {
    Zem_init(&s, 0); 
    printf("parent: beginn");
    pthread_t c;
    Pthread_create(&c, NULL, child, NULL);
    Zem_wait(&s); // wait here for child
    printf("parent: endn");
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号