semaphore_producer_consumer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. while(1)
  50. {
  51. /* 获取一个满位 */
  52. rt_sem_take(&sem_full, RT_WAITING_FOREVER);
  53. /* 临界区,上锁进行操作 */
  54. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  55. sum += array[get%MAXSEM];
  56. rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] );
  57. get++;
  58. rt_sem_release(&sem_lock);
  59. /* 释放一个空位 */
  60. rt_sem_release(&sem_empty);
  61. /* 生产者生产到100个数目,停止,消费者线程相应停止 */
  62. if (get == 100) break;
  63. /* 暂停一小会时间 */
  64. rt_thread_delay(10);
  65. }
  66. rt_kprintf("the consumer[%d] sum is %d \n ", no, sum);
  67. rt_kprintf("the consumer[%d] exit!\n");
  68. }
  69. int semaphore_producer_consumer_init()
  70. {
  71. /* 初始化3个信号量 */
  72. rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO);
  73. rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO);
  74. rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO);
  75. /* 创建线程1 */
  76. producer_tid = rt_thread_create("producer",
  77. producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */
  78. THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  79. if (producer_tid != RT_NULL)
  80. rt_thread_startup(producer_tid);
  81. else
  82. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  83. /* 创建线程2 */
  84. consumer_tid = rt_thread_create("consumer",
  85. consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */
  86. THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  87. if (consumer_tid != RT_NULL)
  88. rt_thread_startup(consumer_tid);
  89. else
  90. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  91. return 0;
  92. }
  93. #ifdef RT_USING_TC
  94. static void _tc_cleanup()
  95. {
  96. /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
  97. rt_enter_critical();
  98. /* 删除线程 */
  99. if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE)
  100. rt_thread_delete(producer_tid);
  101. if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE)
  102. rt_thread_delete(consumer_tid);
  103. /* 调度器解锁 */
  104. rt_exit_critical();
  105. /* 设置TestCase状态 */
  106. tc_done(TC_STAT_PASSED);
  107. }
  108. int _tc_semaphore_producer_consumer()
  109. {
  110. /* 设置TestCase清理回调函数 */
  111. tc_cleanup(_tc_cleanup);
  112. semaphore_producer_consumer_init();
  113. /* 返回TestCase运行的最长时间 */
  114. return 100;
  115. }
  116. /* 输出函数命令到finsh shell中 */
  117. FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example);
  118. #else
  119. /* 用户应用入口 */
  120. int rt_application_init()
  121. {
  122. semaphore_producer_consumer_init();
  123. return 0;
  124. }
  125. #endif