【问题标题】:FreeRTOS SemaphoreFreeRTOS 信号量
【发布时间】:2013-01-04 21:24:37
【问题描述】:

这里是关于 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 );
}

【问题讨论】:

    标签: c semaphore freertos


    【解决方案1】:

    作为您链接到的示例,在 if (...) 正文中采用信号量。如果您从该示例中复制粘贴,则需要确保您的程序中同时具有 xSemaphoreTake 和 xSemaphoreGive。

    【讨论】:

      【解决方案2】:

      当您调用 xSemaphoreTake() 时,您不知道您是否拥有 xSemaphore 信号量。如果它是空闲的,您的代码将继续执行,如果它会在 10 个滴答声内空闲,您的代码将继续执行,而 OS 调度程序不会让您失望(即在您的调用中指定的超时),如果 xSemaphore 在指定的超时后不可用,您的任务将进入阻塞状态,并执行下一个具有更高优先级的就绪任务。

      您对同一个信号量的显式调用 xSemaphoreGive 在这里将是一个严重的错误,如果您不拥有它,那么释放它是没有意义的。

      【讨论】:

        【解决方案3】:

        我的问题是:我是否已经在这里有了信号量,还是必须像这样显式调用 xSemaphoreTake(xSemaphore, (portTickType) 10):

        是的,如果您输入 if 语句的主体,您就有信号量。如果在阻塞时间(在您的情况下为 10 个滴答声)之后信号量不可用(或在持续时间内给出),则 xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) 返回 pdFALSE。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 2022-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-24
          • 1970-01-01
          相关资源
          最近更新 更多