Consider two tasks that synchronize using an event group. Task A sets bit 0, and Task B waits for bit 0 before setting bit 1. What will be the output of the event bits after both tasks run?
EventGroupHandle_t xEventGroup = NULL;
void TaskA(void *pvParameters) {
xEventGroupSetBits(xEventGroup, (1 << 0));
vTaskDelete(NULL);
}
void TaskB(void *pvParameters) {
xEventGroupWaitBits(xEventGroup, (1 << 0), pdFALSE, pdTRUE, portMAX_DELAY);
xEventGroupSetBits(xEventGroup, (1 << 1));
vTaskDelete(NULL);
}
int main() {
xEventGroup = xEventGroupCreate();
xTaskCreate(TaskA, "TaskA", 1000, NULL, 1, NULL);
xTaskCreate(TaskB, "TaskB", 1000, NULL, 1, NULL);
vTaskStartScheduler();
EventBits_t bits = xEventGroupGetBits(xEventGroup);
printf("Event bits: 0x%02X\n", bits);
return 0;
}Remember that Task B waits for bit 0 before setting bit 1, so both bits should be set at the end.
Task A sets bit 0. Task B waits for bit 0, then sets bit 1. So both bits 0 and 1 are set, resulting in 0x03.
What is the output of the event bits after a task waits for bits 0 and 1 with the clear-on-exit option enabled?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 1)); EventBits_t bits = xEventGroupWaitBits(xEventGroup, (1 << 0) | (1 << 1), pdTRUE, pdTRUE, 0); EventBits_t after_wait = xEventGroupGetBits(xEventGroup); printf("Bits returned by wait: 0x%02X\n", bits); printf("Bits after wait: 0x%02X\n", after_wait);
Clear-on-exit means bits are cleared after the wait returns.
The wait returns the bits that were set (0x03). Because clear-on-exit is true, those bits are cleared after the wait, so the bits after wait are 0x00.
What error will occur in the following code snippet that uses an event group for synchronization?
EventGroupHandle_t xEventGroup;
void Task(void *pvParameters) {
xEventGroupSetBits(xEventGroup, (1 << 0));
vTaskDelete(NULL);
}
int main() {
xTaskCreate(Task, "Task", 1000, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}Check if the event group is created before use.
The event group handle is declared but never created with xEventGroupCreate(), so it is NULL. Using it causes a runtime crash.
Three tasks set bits 0, 1, and 2 respectively on the same event group. What is the final value of the event bits?
EventGroupHandle_t xEventGroup = xEventGroupCreate();
void Task0(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 0)); vTaskDelete(NULL); }
void Task1(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 1)); vTaskDelete(NULL); }
void Task2(void *pvParameters) { xEventGroupSetBits(xEventGroup, (1 << 2)); vTaskDelete(NULL); }
int main() {
xTaskCreate(Task0, "Task0", 1000, NULL, 1, NULL);
xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
EventBits_t bits = xEventGroupGetBits(xEventGroup);
printf("Event bits: 0x%02X\n", bits);
return 0;
}Bits 0, 1, and 2 correspond to 0x01, 0x02, and 0x04 respectively.
Setting bits 0, 1, and 2 results in bits 0x01 | 0x02 | 0x04 = 0x07.
Two tasks wait on the same event group bits 0 and 1 with the clear-on-exit option enabled. If bit 0 and bit 1 are set once, what happens to the second task waiting for those bits?
Consider how clear-on-exit affects bits after the first task unblocks.
With clear-on-exit, the first task to unblock clears the bits. The second task then waits indefinitely because the bits are no longer set.
