• 多条件变量应用--传送与搬运问题


    1.描述

    一个货运装置,传送带会将一件货物传送过来,货物传送过来以后,装货员会将货物装上车
    这件货物装车后,传送带会将下一件货物传送过来,然后装货员会再将物装装上车
    装上8件物品后,车子装满,传送带会停止传送货物

    利用多线程方式实现物品传送和装货的次序

    根据给出的代码文件,阅读基本代码文件含义,实现空缺部分的代码
    代码目的:通过创建两个线程来实现对一个数的递加,在“TODO”处添加代码

    Output:
    正在传送第 (1) 个货物
    正在装车第 (1) 个货物
    正在传送第 (2) 个货物
    正在装车第 (2) 个货物
    正在传送第 (3) 个货物
    正在装车第 (3) 个货物
    正在传送第 (4) 个货物
    正在装车第 (4) 个货物
    正在传送第 (5) 个货物
    正在装车第 (5) 个货物
    正在传送第 (6) 个货物
    正在装车第 (6) 个货物
    正在传送第 (7) 个货物
    正在装车第 (7) 个货物
    正在传送第 (8) 个货物
    正在装车第 (8) 个货物
    本次装车结束
    本次装车结束

    2.代码

    1. #include
    2. #include
    3. #include
    4. #define COUNT 8 //车子装满货物的数量
    5. static int int_conveying_number = 0; // 已经传送了多少货物
    6. static int int_loading_number = 0; // 已经装车了多少货物
    7. pthread_mutex_t lock;
    8. pthread_cond_t can_conveying; // 货物传送。
    9. pthread_cond_t can_loading; // 货物装车
    10. void *loading(void *data)
    11. {
    12. /* TODO: write code as below */
    13. /* TODO: 货物装车,装车完成后通知传送货物 */
    14. while (int_loading_number < COUNT) {
    15. pthread_mutex_lock(&lock);
    16. while (int_loading_number >= int_conveying_number) {
    17. pthread_cond_wait(&can_conveying, &lock);
    18. }
    19. printf("正在装车第 (%d) 个货物\n", ++int_loading_number);
    20. pthread_mutex_unlock(&lock);
    21. pthread_cond_signal(&can_loading);
    22. }
    23. printf("本次装车结束\n");
    24. return NULL;
    25. }
    26. void *conveying(void *data)
    27. {
    28. /* TODO: write code as below */
    29. /* TODO: 传送货物,货物传送过来后通知装车 */
    30. while (int_conveying_number < COUNT) {
    31. pthread_mutex_lock(&lock);
    32. while (int_conveying_number > int_loading_number) {
    33. pthread_cond_wait(&can_loading, &lock);
    34. }
    35. printf("正在传送第 (%d) 个货物\n", ++int_conveying_number);
    36. pthread_mutex_unlock(&lock);
    37. pthread_cond_signal(&can_conveying);
    38. }
    39. printf("本次传送结束\n");
    40. return NULL;
    41. }
    42. int main(void)
    43. {
    44. pthread_t th_conveyor, th_worker;
    45. void *retval;
    46. pthread_mutex_init(&lock, NULL);
    47. pthread_cond_init(&can_conveying, NULL);
    48. pthread_cond_init(&can_loading, NULL);
    49. pthread_create(&th_conveyor, NULL, conveying, NULL);
    50. pthread_create(&th_worker, NULL,loading, NULL);
    51. /* 等待装车完成,结束*/
    52. pthread_join(th_conveyor, &retval);
    53. pthread_join(th_worker, &retval);
    54. pthread_mutex_destroy(&lock);
    55. pthread_cond_destroy(&can_conveying);
    56. pthread_cond_destroy(&can_loading);
    57. return 0;
    58. }

    3.编译

    Makefile:

    1. DBG = #-ggdb3
    2. OPTFLAGS = -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes $(DBG) -pedantic
    3. SRC = convery_and_load.c
    4. TARGET = convery_and_load
    5. OBJ = $(SRC:.c=.o)
    6. default: $(TARGET)
    7. LDFLAGS += -lpthread
    8. %.o: %.c
    9. $(CC) $(CFLAGS) -c -o $@ $<
    10. $(TARGET): $(OBJ)
    11. $(CC) -o $@ $(OBJ) $(LDFLAGS)
    12. clean:
    13. rm $(TARGET)

     

  • 相关阅读:
    显卡---显卡驱动---CUDA---Cudnn
    lintcode 1489 · 最大矩阵边界和 【前缀和数组 中等 vip】
    【数据挖掘】2020奇安信秋招算法方向试卷3 笔试题解析
    二分图匹配(匈牙利算法 DFS 实现)
    Vue计算属性的使用
    SpringBoot学习(5) —— SpringBoot框架集成MyBatis
    国际邮箱推荐:不容错过的企业邮箱选择
    【Python】批量下载素材酷视频资源
    南卡和声阔真无线降噪耳机哪款更好?南卡和声阔蓝牙耳机测评
    【软件安装】Linux系统中安装MySQL数据库服务
  • 原文地址:https://blog.csdn.net/wangzhicheng2013/article/details/128197121