以下是关于xSemaphoreTake()函数的FreeRTOS api-reference http://www.freertos.org/a00122.html的摘录:
// See if we can obtain the semaphore. If the semaphore is not available
// wait 10 ticks to see if it becomes free.
if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
{
// We were able to obtain the semaphore and can now access the
// shared resource.
我的问题是:我是否已经拥有信号量或者我必须打电话
xSemaphoreTake( xSemaphore, (portTickType) 10 )
明确表示:
// We have finished accessing the shared resource. Release the
// semaphore.
xSemaphoreGive( xSemaphore );
}
答案 0 :(得分:4)
在您链接到的示例中,在if(...)正文中获取信号量。如果你是从那个例子中复制粘贴的,那么你可以确保在你的程序中同时拥有xSemaphoreTake和xSemaphoreGive。
答案 1 :(得分:0)
调用xSemaphoreTake()时,您不知道是否拥有xSemaphore信号量。如果它是空闲的,那么你的代码将继续执行,如果它在10个时钟内变为空闲,你的代码将继续执行而不会让OS调度程序让你失望(这是你的调用中指定的超时),如果xSemaphore在指定的超时后不可用,您的任务将进入阻塞状态,下一个具有更高优先级的就绪任务将执行。
你明确调用xSemaphoreGive并引用相同的信号量会在这里出现严重错误,如果你没有拥有它,发布它就没有意义。
答案 2 :(得分:0)
我的问题是:我是否已经拥有信号量或者我必须将xSemaphoreTake(xSemaphore,(portTickType)10)明确地称为:
是的,如果输入if语句的主体,则您具有信号量。如果在阻塞时间(在你的情况下为10个滴答)之后信号量不可用(或在持续时间内给出),则xSemaphoreTake(xSemaphore,(portTickType)10)返回pdFALSE。