UlTaskNotifyTake() Not Blocking After xTaskNotifyStateClear()

UlTaskNotifyTake() Not Blocking After xTaskNotifyStateClear()

The official FreeRTOS docs give an example on how to clear a task's notification state with xTaskNotifyStateClear() before waiting for a notification with ulTaskNotifyTake() to make sure ulTaskNotifyTake() doesn't return instantly due to previous notification:

In short:

xTaskNotifyStateClear(NULL);
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // Should block according to example

When I tried this in my code, ulTaskNotifyTake() returned immediately, though. After reading the function's docs (), I think it does so because xTaskNotifyStateClear(NULL) only clears the current task's notification status but not the notification value and ulTaskNotifyTake() only looks for the latter. The following code works for me and confirms my assumption:

xTaskNotifyStateClear(NULL);
ulTaskNotifyValueClear(NULL, UINT_MAX);
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // Does block

Is my understanding, that ulTaskNotifyTake() doesn't block as long as the notification value is greater 0 correct? Is the official example () incorrect then, as the call to xTaskNotifyStateClear() doesn't guarantee ulTaskNotifyTake() to block? Should you use ulTaskNotifyValueClear() or xTaskNotifyWait() in the example, instead?

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.