AnsweredAssumed Answered

When to use events vs. semaphores

Question asked by Michael Schwager on May 27, 2014
Latest reply on May 29, 2014 by Michael Schwager

Hi I'm trying to figure out when to use events vs. semaphores.  It seems that when semaphore max count == 1, it's the same as an event in case 1, but not in case 2.  Here are a few use cases.

 

1. Main task initializes several other tasks, or several copies of the same task, then waits for each one to post its semaphore.  Main task has to wait N times for N tasks to finish posting.  Not sure if this is the right way to do this sort of thing.  Sounds like I can use either events or semaphores here:

main_task(...) {

_task_create(0, DEVICE_RUN_TASK, (uint32_t)(&semaphore0));
_task_create(0, TIMER_TASK,       (uint32_t)(&semaphore1));
_task_create(0, TIMER_TASK,       (uint32_t)(&semaphore2));

 

// Wait for start semaphores to come back, one per task.
_lwsem_wait(&semaphore0);
_lwsem_wait(&semaphore1);
_lwsem_wait(&semaphore2);

}

 

2. Several slave tasks are waiting for master task to complete something.  Master task has to post multiple semaphores, which is not good because the task should be agnostic of who's listening, and anyway the semantics are that all the slave tasks should see the event as an atomic thing, ie simultaneously, if only in theory and not in practice.  So it sounds like I must use an event.  Not sure how to ensure that each waiting task sees the same event at most exactly once.  Is this what autoclearing is for?

Outcomes