信号量的使用
struct rt_semaphore
{
struct rt_ipc_object parent; /**< inherit from ipc_object */
rt_uint16_t value; /**< value of semaphore. */
};
定义静态信号量:struct rt_semaphore static_sem;
定义动态信号量:rt_sem_t dynamic_sem;(一个指针)
初始化与脱离:
rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
rt_err_t rt_sem_detach(rt_sem_t sem)
创建与删除(与初始化的区别,注意观察参数)
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag) RT_IPC_FLAG_FIFO RT_IPC_FLAG_PRIO
rt_err_t rt_sem_delete(rt_sem_t sem)
获取信号量
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) //RT_WAITING_FOREVER=-1
rt_err_t rt_sem_trytake(rt_sem_t sem)
释放信号量(信号量的值加1)
rt_err_t rt_sem_release(rt_sem_t sem)
生产者与消费者
生产者关注缓冲区的空位数量
消费者关注缓冲区的可用数量
缓冲区同一时刻只能一个线程访问