About Freertos: How to Define the xBlockTime Value in the "Xsemaphoretake" Api?

Firstly, the declaration in FreeRTOS is like this:

#define xSemaphoreTake( xSemaphore, xBlockTime )    xQueueSemaphoreTake( ( xSemaphore ), ( xBlockTime ) )

In the current project that I am working on, the value of the xBlockTime parameter in the "xSemaphoreTake" API is assigned randomly: sometimes it is 5, or it is 10, 200, 1000, and portMAX_DELAY, etc.

So how to determine the correct (or the best) value of this parameter in your eyes?

1 Answer

As mentioned in the documentation for xSemaphoreTake, the second parameter is "The time in ticks to wait for the semaphore to become available". If the semaphore is not available after the specified time interval, then xSemaphoreTake returns pdFALSE.

The range of appropriate values depends entirely on the application requirements. Semaphores are generally used to guard against multiple tasks accessing some resource simultaneously. What happens if the resource isn't available in a timely manner? Should you ring an alarm bell to alert the operator? Log an error message somewhere? Raise a flag? Or insert a hard debugger breakpoint in debug builds to ease troubleshooting? If there's no hurry, then the appropriate thing can be to sleep until the resource is available, so we can set the block time to portMAX_DELAY.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest