C++ wrappers, examples needed, please

Hi,

I wrote an article about monitor semaphores in C++ using FreeRTOS mutex semaphores. Maybe it’s what you’re looking for, or something closer.

I also wrote a pair of articles (here and here) about how to objectify the FreeRTOS timers using dynamic and static (CRTP) polymorphism, respectively.

I haven’t objectyfied (and I won’t) all FreeRTOS API 'cause it’s a very hard work; however, you can grab the idea on how to convert some FreeRTOS features/mechanisms to classes and objects so you use/write what you need and adapt to your needs what’s need it.

You are using the non existent term “mutex semaphore” which does nothing but add to the confusion about mutual exclusion that has been discussed here too many times to mention. There is no such thing as a “mutex semaphore.”

Also, your “article” has severe errors (for example, your explanation about he semantics of muteces is simply wrong).

You are using the non existen term “Muteces” (it’s a word that actually doesn’t exist), and as you’ve used it twice then I’m guessing that you have understanding problems.

A mutex (hereinafter simply mutex ) is a binary semaphore with two outstanding features:…

Crystal clear. That’s all? What else?

What do you mean with the “semantic of muteces”? If “muteces” means “mutexes” then you need to read this FreeRTOS itself explanation.

Besides the “Mutex semaphore” confussion, everything else is documented, I’d like that someone that can’t spell correctly “mutexes” points me out to the “severe errors” found in my post. Really, do it!

Everything is wrong about that, especially if one quotes further:


A mutex (hereinafter simply mutex) is a binary semaphore with two outstanding features:

  • The semaphore is given and it MUST be returned.
  • The priority of the task that gets the mutex might be temporarily raised so that the task is not interrupted while it owns it so that it can finish its work as soon as possible.

First of all, a mutex is NOT a semaphore and NOT a variation of it. There is a semantic resemblance between mutexes (yeah, kill me for a typo) and binary semaphores, but the two are distinct concepts. None is a variation of the other.

Second, no synchronization object is “returned.” Semaphores or mutexes are “given” or “released.” This is not splitting in hairs because “to return” implies “to give to someone who has had it before,” which is NOT the case for synchronization objects. If a thread releases a mutex, it simply gives up ownership of the mutex for another thread to claim. If no other thread happens to be suspended on the mutex, the mutex is “given to nothingness,” thus NOT RETURNED! The language used to describe concurrency has evolved over decades and has implied meanings which you can not simply disregard. I dare you to find any meaningful textbook on concurrency which uses the term “to return a synchronization object.”

Thirdly, your statement “The semaphore is given and it MUST be returned.” is completly meaningless, as “given” and “returned” would be the same operation if “returned” would be valid. What you might have wanted to write is something like “a mutex must be claimed and released by the same thread”. Whatever you wanted to express with your sentence does not describe anything specific to a mutex.

Fourth, the other distinction you mention is incorrect. A mutex CAN support priority inheritance, but it does not have to. The more important distinction (of which priority inheritance support is a side effect) is that the ownership of a mutex is being tracked by the OS.

You are being very sloppy and superficial in about every single sentence.

Also, it is rather pathetic to try to pawn of a single typo of mine (which even isn’t one; there are usages of muteces as mutex plurals, even though that is uncommon) in a forum contribution against complete nonsense of yours in a published article.

1 Like

Considering a mutex to be a type of semaphore (commonly, a special type of binary semaphore) is no big deal. It’s done all the time. Literature does it. Academia does it. Industry does it. Best to just interpret into your way of thinking and move forward.

1 Like

RAc, Mutexes ARE a form of generic Semaphore, being defined as a basic form of flag control, and in fact, in FreeRTOS they use most of the same code. (Note, there is no “mutex.h” header in FreeRTOS). Note, you even use “Semaphore” functions to give and take a Mutex.

Yes, there are usage differences because a Mutex is “more specialized” to give it some additional capabilities, which also give it additional usage restrictions, but a Mutex really is just a variation of a Binary Semaphore.

Also, the phrase “Mutex Semaphore”, while perhaps redundant is used at time when comparing an contrasting it with the other Semaphores, the Binary and Counting.

I will also say I HAVE heard of the terminology of “Returning” a mutex, and what it is being returned to is the “Available Pool” (this is more often used with pool resources where you attempt to “take” an item, and you don’t care which one you get, and if one is available you get a particular one with exclusive control, and when you are done, you “return” it to the pool. (These becomes a bit more like a Counting Semaphore, except it still keeps ownership of particulars.

This can be one problem with technical fields, sometimes they are more divided than you might realize.

1 Like

just because they share code doesn’t mean there is a relationship. If that was a valid argument, you could also argue that queues and semaphores are variations of each other. In FreeRTOS they happen to be (because queues are the basis of almost everything) but only in terms of implementation, by no means necessarily in terms of semantics.

Yes. You think I haven’t noticed that yet? It’s one of the few gripes I have about the FreeRTOS API nomenclature because it blurs the lines between what things are.

Hold on. Earlier on in your post you wrote “Mutexes ARE a form of generic Semaphore,” but now you write “a Mutex is “more specialized [then a semaphore]…”” ? Aren’t those statements mutually exclusive (pun intended)?

Please give me a reference to any serious (!) credible source that uses it this way!

Do you see any indication that this is the way Xavier intended this phrasing to read? Would you mind giving me your personal assessment of his sentence " The semaphore is given and it MUST be returned." and how that can be interpreted to explain what a mutex is?

One of the problems with language is that I can be used to convey messages on many levels. YOUR answer is not intended to bring any clarification to the issue, even though you may not realize it.

This discussion has got a bit academic and wondered off topic for the thread, hence I’m going to close.

1 Like