Sync of different actions using a recursive mutex?

gerhardkreuzer wrote on Sunday, July 15, 2018:

Hi,
I have to sync a task with more than one action, what is the recommended way to do this?

I start some timers and a task.

After a timer expires some code is executed and when all the timer have done their job the waiting task shuld be unlocked to do some work.

The order of the timers expiration isn’t known, so I need a solution which takes this into account.

One idea: When a timer is started it takes a recursive mutex and gives it back when the timer exoires. The task awaits the point in time when the last timer has giveb back the mutex.

With best regards

Gerhard

richard_damon wrote on Sunday, July 15, 2018:

To wait on ALL of a set of conditions, you can just wait for each of them in sequence, and you will finally fall through when all have been given.

You probably can’t use a mutex, but would need to use a semaphore or direct to task notification as a mutex needs to given by the same task that take is, but timer callbacks run in the Timer task, which (unless you started it from a timer callback) wasn’t where the timer was started.

rtel wrote on Sunday, July 15, 2018:

Alternatively use an event group - each timer sets a different bit in
the event group and the waiting task blocks until all bits are set.
https://www.freertos.org/xEventGroupWaitBits.html

sergiomartin wrote on Monday, July 16, 2018:

You can wait in a Queue and other part of the code send a message in this queue with the information that you need

jeronimo479 wrote on Wednesday, July 18, 2018:

Great answer! Exactly what I was thinking. The I think that a recursive Mutex would fail anyway, because that only applies to the same task taking the mutex over and over, not different tasks trying to take the same mutex (which is exactly what a Mutex prevents). I would think that after the first timer took the mutex, the next one would block and the timer wouldn’t actually run.