浅谈生产者消费者模型(Linux系统下的两种实现方法)

2025-05-27 0 42

生产者消费者问题是同步问题中的一种常见情况,借用一下维基百科的话

生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bounded-buffer problem),是一个多线程同步问题的经典案例。该问题描述了两个共享固定大小缓冲区的线程——即所谓的“生产者”和“消费者”——在实际运行时会发生的问题。生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。与此同时,消费者也在缓冲区消耗这些数据。该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。

第一种实现信号量配合互斥锁实现,这种方法很清晰简单

信号量:

信号量的特性如下:信号量是一个非负整数(车位数),所有通过它的线程/进程(车辆)都会将该整数减一(通过它当然是为了使用资源),当该整数值为零时,所有试图通过它的线程都将处于等待状态。在信号量上我们定义两种操作: Wait(等待) 和 Release(释放)。当一个线程调用Wait操作时,它要么得到资源然后将信号量减一,要么一直等下去(指放入阻塞队列),直到信号量大于等于一时。Release(释放)实际上是在信号量上执行加操作,对应于车辆离开停车场,该操作之所以叫做“释放”是因为释放了由信号量守护的资源。

wait, release在Linux

int sem_wait(sem_t * sem);
int sem_post(sem_t * sem);

设定两个信号量,empty用来表示空槽的个数,full用来表示占有的个数

生产者在向任务队列里放资源时,调用sem_wait(&full)来检查队列是否已满,如果满的话,就阻塞,直到有消费者从里面取资源再苏醒,如果不满,就放资源,并通知消费者来取。

消费者在从任务队列里取资源时,调用sem_wait(&empty)来检查队列是否为空,如果空的话,就阻塞,直到有生产者向里面放资源再苏醒,如果不空,就取资源,并通知生产者来放。

而互斥锁仅仅是为了防止多个线程同时对队列进行操作,造成未知的结果。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81
#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

#define MAX 5 //队列长度

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

sem_t full; //填充的个数

sem_t empty; //空槽的个数

int top = 0; //队尾

int bottom = 0; //队头

void* produce(void* arg)

{

int i;

for ( i = 0; i < MAX*2; i++)

{

printf("producer is preparing data\\n");

sem_wait(&empty);//若空槽个数低于0阻塞

pthread_mutex_lock(&mutex);

top = (top+1) % MAX;

printf("now top is %d\\n", top);

pthread_mutex_unlock(&mutex);

sem_post(&full);

}

return (void*)1;

}

void* consume(void* arg)

{

int i;

for ( i = 0; i < MAX*2; i++)

{

printf("consumer is preparing data\\n");

sem_wait(&full);//若填充个数低于0阻塞

pthread_mutex_lock(&mutex);

bottom = (bottom+1) % MAX;

printf("now bottom is %d\\n", bottom);

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

return (void*)2;

}

int main(int argc, char *argv[])

{

pthread_t thid1;

pthread_t thid2;

pthread_t thid3;

pthread_t thid4;

int ret1;

int ret2;

int ret3;

int ret4;

sem_init(&full, 0, 0);

sem_init(&empty, 0, MAX);

pthread_create(&thid1, NULL, produce, NULL);

pthread_create(&thid2, NULL, consume, NULL);

pthread_create(&thid3, NULL, produce, NULL);

pthread_create(&thid4, NULL, consume, NULL);

pthread_join(thid1, (void**)&ret1);

pthread_join(thid2, (void**)&ret2);

pthread_join(thid3, (void**)&ret3);

pthread_join(thid4, (void**)&ret4);

return 0;

}

注:如果把sem_wait()和sem_post()放到pthread_mutex_lock()与pthread_mutex_unlock()之间会如何呢?

答案是:死锁,因为我们不能预知线程进入共享区顺序,如果消费者线程先对mutex加锁,并进入,sem_wait()发现队列为空,阻塞,而生产者在对mutex加锁时,发现已上锁也阻塞,双方永远无法唤醒对方。

第二种是条件变量配合互斥锁实现

条件变量的常见用法是在不满足某些条件时,阻塞自己,直到有线程通知自己醒来。

而互斥量在这里的作用依然还是防止多线程对共享资源同时操作,造成未知结果。

生产者消费者的行为与之前相同,只不过原来只调用sem_wait()可以完成两步,1是检查条件,2是阻塞,现在条件变量需要我们自己来设定条件(所以说条件变量配合互斥锁比信号量的功能更强大,因为它可以自定义休眠条件,但是这对使用者的要求也提高了,必须理清逻辑关系避免死锁)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78
#include <stdio.h>

#include <pthread.h>

#define MAX 5

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t notfull = PTHREAD_COND_INITIALIZER; //是否队满

pthread_cond_t notempty = PTHREAD_COND_INITIALIZER; //是否队空

int top = 0;

int bottom = 0;

void* produce(void* arg)

{

int i;

for ( i = 0; i < MAX*2; i++)

{

pthread_mutex_lock(&mutex);

while ((top+1)%MAX == bottom)

{

printf("full! producer is waiting\\n");

pthread_cond_wait(¬full, &mutex);//等待队不满

}

top = (top+1) % MAX;

printf("now top is %d\\n", top);

pthread_cond_signal(¬empty);//发出队非空的消息

pthread_mutex_unlock(&mutex);

}

return (void*)1;

}

void* consume(void* arg)

{

int i;

for ( i = 0; i < MAX*2; i++)

{

pthread_mutex_lock(&mutex);

while ( top%MAX == bottom)

{

printf("empty! consumer is waiting\\n");

pthread_cond_wait(¬empty, &mutex);//等待队不空

}

bottom = (bottom+1) % MAX;

printf("now bottom is %d\\n", bottom);

pthread_cond_signal(¬full);//发出队不满的消息

pthread_mutex_unlock(&mutex);

}

return (void*)2;

}

int main(int argc, char *argv[])

{

pthread_t thid1;

pthread_t thid2;

pthread_t thid3;

pthread_t thid4;

int ret1;

int ret2;

int ret3;

int ret4;

pthread_create(&thid1, NULL, produce, NULL);

pthread_create(&thid2, NULL, consume, NULL);

pthread_create(&thid3, NULL, produce, NULL);

pthread_create(&thid4, NULL, consume, NULL);

pthread_join(thid1, (void**)&ret1);

pthread_join(thid2, (void**)&ret2);

pthread_join(thid3, (void**)&ret3);

pthread_join(thid4, (void**)&ret4);

return 0;

}

注:

为什么信号量在互斥区外,而条件变量在互斥区内呢?

因为互斥锁本质上是二元信号量,和信号量互斥的原理相同,而且放在互斥区会死锁,而条件变量是和互斥锁协同配合的,

我们从pthread_cond_wait()和pthread_cond_signal()的内部实现就可以看出

pthread_cond_wait()是先将互斥锁解开,并陷入阻塞,直到pthread_signal()发出信号后pthread_cond_wait()再加上锁,然后退出,可以看到它们在设计时就是为了协同配合,而互斥锁和信号量都是由Linux下的futex机制实现的,这里就不展开说了

这里贴出了pthread_wait()源码图

浅谈生产者消费者模型(Linux系统下的两种实现方法)

以上就是小编为大家带来的浅谈生产者消费者模型(Linux系统下的两种实现方法)全部内容了,希望大家多多支持快网idc~

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 浅谈生产者消费者模型(Linux系统下的两种实现方法) https://www.kuaiidc.com/61323.html

相关文章

发表评论
暂无评论