semaphore_producer_consumer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * 程序清单:生产者消费者例子
  3. *
  4. * 这个例子中将创建两个线程用于实现生产者消费者问题
  5. */
  6. #include <rtthread.h>
  7. #include "tc_comm.h"
  8. /* 定义最大5个元素能够被产生 */
  9. #define MAXSEM 5
  10. /* 用于放置生产的整数数组 */
  11. rt_uint32_t array[MAXSEM];
  12. /* 指向生产者、消费者在array数组中的读写位置 */
  13. static rt_uint32_t set, get;
  14. /* 指向线程控制块的指针 */
  15. static rt_thread_t producer_tid = RT_NULL;
  16. static rt_thread_t consumer_tid = RT_NULL;
  17. struct rt_semaphore sem_lock;
  18. struct rt_semaphore sem_empty, sem_full;
  19. /* 生成者线程入口 */
  20. void producer_thread_entry(void* parameter)
  21. {
  22. int cnt = 0;
  23. /* 运行100次 */
  24. while( cnt < 100)
  25. {
  26. /* 获取一个空位 */
  27. rt_sem_take(&sem_empty, RT_WAITING_FOREVER);
  28. /* 修改array内容,上锁 */
  29. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  30. array[set%MAXSEM] = cnt + 1;
  31. rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]);
  32. set++;
  33. rt_sem_release(&sem_lock);
  34. /* 发布一个满位 */
  35. rt_sem_release(&sem_full);
  36. cnt++;
  37. /* 暂停一段时间 */
  38. rt_thread_delay(50);
  39. }
  40. rt_kprintf("the producer exit!\n");
  41. }
  42. /* 消费者线程入口 */
  43. void consumer_thread_entry(void* parameter)
  44. {
  45. rt_uint32_t no;
  46. rt_uint32_t sum;
  47. /* 第n个线程,由入口参数传进来 */
  48. no = (rt_uint32_t)parameter;
  49. sum = 0;
  50. while(1)
  51. {
  52. /* 获取一个满位 */
  53. rt_sem_take(&sem_full, RT_WAITING_FOREVER);
  54. /* 临界区,上锁进行操作 */
  55. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  56. sum += array[get%MAXSEM];
  57. rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] );
  58. get++;
  59. rt_sem_release(&sem_lock);
  60. /* 释放一个空位 */
  61. rt_sem_release(&sem_empty);
  62. /* 生产者生产到100个数目,停止,消费者线程相应停止 */
  63. if (get == 100) break;
  64. /* 暂停一小会时间 */
  65. rt_thread_delay(10);
  66. }
  67. rt_kprintf("the consumer[%d] sum is %d \n ", no, sum);
  68. rt_kprintf("the consumer[%d] exit!\n");
  69. }
  70. int semaphore_producer_consumer_init()
  71. {
  72. /* 初始化3个信号量 */
  73. rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO);
  74. rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO);
  75. rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO);
  76. /* 创建线程1 */
  77. producer_tid = rt_thread_create("producer",
  78. producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */
  79. THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  80. if (producer_tid != RT_NULL)
  81. rt_thread_startup(producer_tid);
  82. else
  83. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  84. /* 创建线程2 */
  85. consumer_tid = rt_thread_create("consumer",
  86. consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */
  87. THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  88. if (consumer_tid != RT_NULL)
  89. rt_thread_startup(consumer_tid);
  90. else
  91. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  92. return 0;
  93. }
  94. #ifdef RT_USING_TC
  95. static void _tc_cleanup()
  96. {
  97. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  98. rt_enter_critical();
  99. rt_sem_detach(&sem_lock);
  100. rt_sem_detach(&sem_empty);
  101. rt_sem_detach(&sem_full);
  102. /* 删除线程 */
  103. if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE)
  104. rt_thread_delete(producer_tid);
  105. if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE)
  106. rt_thread_delete(consumer_tid);
  107. /* 调度器解锁 */
  108. rt_exit_critical();
  109. /* 设置TestCase状态 */
  110. tc_done(TC_STAT_PASSED);
  111. }
  112. int _tc_semaphore_producer_consumer()
  113. {
  114. /* 设置TestCase清理回调函数 */
  115. tc_cleanup(_tc_cleanup);
  116. semaphore_producer_consumer_init();
  117. /* 返回TestCase运行的最长时间 */
  118. return 100;
  119. }
  120. /* 输出函数命令到finsh shell中 */
  121. FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example);
  122. #else
  123. /* 用户应用入口 */
  124. int rt_application_init()
  125. {
  126. semaphore_producer_consumer_init();
  127. return 0;
  128. }
  129. #endif