producer_consumer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * 程序清单:生产者消费者例子
  12. *
  13. * 这个例子中将创建两个线程用于实现生产者消费者问题
  14. *(1)生产者线程将cnt值每次加1并循环存入array数组的5个成员内;
  15. *(2)消费者线程将生产者中生产的数值打印出来,并累加求和
  16. */
  17. #include <rtthread.h>
  18. #define THREAD_PRIORITY 6
  19. #define THREAD_STACK_SIZE 512
  20. #define THREAD_TIMESLICE 5
  21. /* 定义最大5个元素能够被产生 */
  22. #define MAXSEM 5
  23. /* 用于放置生产的整数数组 */
  24. rt_uint32_t array[MAXSEM];
  25. /* 指向生产者、消费者在array数组中的读写位置 */
  26. static rt_uint32_t set, get;
  27. /* 指向线程控制块的指针 */
  28. static rt_thread_t producer_tid = RT_NULL;
  29. static rt_thread_t consumer_tid = RT_NULL;
  30. struct rt_semaphore sem_lock;
  31. struct rt_semaphore sem_empty, sem_full;
  32. /* 生产者线程入口 */
  33. void producer_thread_entry(void *parameter)
  34. {
  35. int cnt = 0;
  36. /* 运行10次 */
  37. while (cnt < 10)
  38. {
  39. /* 获取一个空位 */
  40. rt_sem_take(&sem_empty, RT_WAITING_FOREVER);
  41. /* 修改array内容,上锁 */
  42. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  43. array[set % MAXSEM] = cnt + 1;
  44. rt_kprintf("the producer generates a number: %d\n", array[set % MAXSEM]);
  45. set++;
  46. rt_sem_release(&sem_lock);
  47. /* 发布一个满位 */
  48. rt_sem_release(&sem_full);
  49. cnt++;
  50. /* 暂停一段时间 */
  51. rt_thread_mdelay(20);
  52. }
  53. rt_kprintf("the producer exit!\n");
  54. }
  55. /* 消费者线程入口 */
  56. void consumer_thread_entry(void *parameter)
  57. {
  58. rt_uint32_t sum = 0;
  59. while (1)
  60. {
  61. /* 获取一个满位 */
  62. rt_sem_take(&sem_full, RT_WAITING_FOREVER);
  63. /* 临界区,上锁进行操作 */
  64. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  65. sum += array[get % MAXSEM];
  66. rt_kprintf("the consumer[%d] get a number: %d\n", (get % MAXSEM), array[get % MAXSEM]);
  67. get++;
  68. rt_sem_release(&sem_lock);
  69. /* 释放一个空位 */
  70. rt_sem_release(&sem_empty);
  71. /* 生产者生产到10个数目,停止,消费者线程相应停止 */
  72. if (get == 10) break;
  73. /* 暂停一小会时间 */
  74. rt_thread_mdelay(50);
  75. }
  76. rt_kprintf("the consumer sum is: %d\n", sum);
  77. rt_kprintf("the consumer exit!\n");
  78. }
  79. int producer_consumer(void)
  80. {
  81. set = 0;
  82. get = 0;
  83. /* 初始化3个信号量 */
  84. rt_sem_init(&sem_lock, "lock", 1, RT_IPC_FLAG_FIFO);
  85. rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO);
  86. rt_sem_init(&sem_full, "full", 0, RT_IPC_FLAG_FIFO);
  87. /* 创建生产者线程 */
  88. producer_tid = rt_thread_create("producer",
  89. producer_thread_entry, RT_NULL,
  90. THREAD_STACK_SIZE,
  91. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  92. if (producer_tid != RT_NULL)
  93. {
  94. rt_thread_startup(producer_tid);
  95. }
  96. else
  97. {
  98. rt_kprintf("create thread producer failed");
  99. return -1;
  100. }
  101. /* 创建消费者线程 */
  102. consumer_tid = rt_thread_create("consumer",
  103. consumer_thread_entry, RT_NULL,
  104. THREAD_STACK_SIZE,
  105. THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  106. if (consumer_tid != RT_NULL)
  107. {
  108. rt_thread_startup(consumer_tid);
  109. }
  110. else
  111. {
  112. rt_kprintf("create thread consumer failed");
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. /* 导出到 msh 命令列表中 */
  118. MSH_CMD_EXPORT(producer_consumer, producer_consumer sample);