2013-01-04 66 views
1

这里是FreeRTOS的API参考http://www.freertos.org/a00122.html关于xSemaphoreTake()函数的摘录:FreeRTOS的旗语

// 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); 
} 

回答

3

就像你链接到的例子,在if(...)体内信号量被采取。如果您是从该示例复制粘贴,则需要确保您的程序中包含xSemaphoreTake和xSemaphoreGive。

0

当您调用xSemaphoreTake()时,您不知道是否拥有xSemaphore信号量。如果它是免费的,你的代码将继续执行,如果它在10个时钟周期内变为空闲的,那么如果在指定的超时后xSemaphore不可用,则代码将继续执行,而OS调度程序不会让你停止运行(即在您的调用中指定的超时时间) ,你的任务将进入阻塞状态,下一个具有更高优先级的就绪任务将被执行。

你明确的调用xSemaphoreGive参考相同的信号量将是一个严重的错误,如果你没有它,它将是没有意义的发布它。

0

我的问题是:我已经有信号灯这里还是我必须调用xSemaphoreTake(xSemaphore,(portTickType)10)明确,如:

是你的信号,如果你进入if语句的主体。如果信号量在阻塞时间之后(在你的情况下为10个时钟周期)不可用(或在持续时间中给出),则xSemaphoreTake(xSemaphore,(portTickType)10)返回pdFALSE。