Is there a way to lock several mutexes?

I have found the ScopedLock<> RAII way to lock mutexes, but I only manage to lock 1 mutex with it. I.e. this does not work:

ScopedLock<Mutex, Mutex> double_lock(mutex_1, mutex_2);

Any idea how to get something providing this behavior?


looking at the header, mbed-os/ScopedLock.h at master · ARMmbed/mbed-os · GitHub it looks like it is only implemented to lock 1 mutex at a time. If you wanted to implement a multi lock and provide a PR to Mbed OS I’m sure it would be considered.



Many thanks for your message :slight_smile: .

I think implementing a scoped_lock would be great. A couple of questions (I am not too familiar with MBedOS):

  • It is fine using variadic templates / functions in MbedOS code, right?

  • One thing I am unsure about, is what kind of deadlock-avoidance algorithm could be used and would fit well to the MbedOS context. I do not have any experience with writing this sort of implementations from before. I suppose even something quite naive would be better than nothing anyways. Any recommendation there?

  • Should these sorts of questions be discussed here on the forum or on a github issue?

I would suggest getting a PR up and then the right part of the Mbed team can take a look, give advice and pointers where necessary .