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


    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)

     

  • 相关阅读:
    linux-Too many open files排查及修复
    PyQt5 不规则窗口的显示
    如何渲染最原始的yuv视频数据?
    C#列表List的创建与使用
    python 2 环境创建及pip install + scipy bug解决
    Google Chrome 任意文件读取 (CVE-2023-4357)漏洞
    【无标题】
    Java责任链模式之总有你想不到的知识
    CDH大数据平台 ModuleNotFoundError: No module named ‘_sqlite3‘
    小程序测试基础知识分享,获取专业测试报告就找卓码软件测评
  • 原文地址:https://blog.csdn.net/wangzhicheng2013/article/details/128197121