News for hackers. Stuff that matters.
Re:Practical Guide to Bare Metal C++ by monocasa on Friday June 02, 2017 @02:04PM (#14472055)
I traditionally haven't been the biggest fan of this book. Most of what they list is stuff that just looks like using C++ just for the sake of it, with no real benefit over a C codebase.

For instance they don't appear to use RAII at all, even in places where it's an obvious win (InterruptLock).

Re:Practical Guide to Bare Metal C++ by butterisgood on Friday June 02, 2017 @05:24PM (#14473425)
The first advantage listed of C++ is "code reuse" which is where I stopped reading.

The first advantage for me of C++ everywhere IS RAII.

Re:Practical Guide to Bare Metal C++ by vowelless on Friday June 02, 2017 @02:23PM (#14472201)
I tried searching the book for RAII and there are no hits...

Do they really not cover it or is the search busted ?

Re:Practical Guide to Bare Metal C++ by mbel on Friday June 02, 2017 @03:10PM (#14472559)
I don't think RAII is a concept complex enough to write a book about it... Probably a blog post or Wikipedia page is enough to explain it fully.
Re:Practical Guide to Bare Metal C++ by monocasa on Friday June 02, 2017 @03:29PM (#14472674)
It's worth a chapter in this book IMO.

As the maintainer of a C++14 proprietary RTOS, it's probably the most convincing argument to the C guys as to why C++ can benefit embedded codebases. Like not being able to forget to re-drop thread priority when you've temporarily increased it.

Or one of my favorite patterns is returning the object representing the temporary higher priority. If the caller doesn't do anything with the return value, the destructor is called and thread priority reverts to what it was. However, it gives you a chance for the caller to hold on to that high priority object, and get more work done essentially atomically.

Re:Practical Guide to Bare Metal C++ by xyzzy_plugh on Friday June 02, 2017 @03:44PM (#14472777)
But the compiler doesn't optimize out the constructor, right? It can't.

In an RTOS of all places, that seems awfully expensive. One generally avoids wasted resources in embedded -- the trade-off is it requires more care during development.

I've never seen RAII work well in an embedded system. Not being able to handle exceptions or errors during destruction ruins everything.

Re:Practical Guide to Bare Metal C++ by monocasa on Friday June 02, 2017 @04:12PM (#14472940)
> But the compiler doesn't optimize out the constructor, right? It can't.

> In an RTOS of all places, that seems awfully expensive. One generally avoids wasted resources in embedded -- the trade-off is it requires more care during development.

The compiler has no issues "optimizing out" the constructor in my experience. The asm looks just like the equivalent C in all of the cases I've seen.

> I've never seen RAII work well in an embedded system. Not being able to handle exceptions or errors during destruction ruins everything.

I mean, we disallow exceptions anyway. And we use RAII for objects where there isn't a weird failure case to be handled like my example of thread priority guards.

Re:Practical Guide to Bare Metal C++ by fdsfsafasfdas on Friday June 02, 2017 @05:04PM (#14473304)
Disabling exceptions has solved this. There are no need for exceptions in the kernel, and the cost is high.
Re:Practical Guide to Bare Metal C++ by concede_pluto on Friday June 02, 2017 @05:48PM (#14473563)
Isn't table-driven stack unwinding cheaper than having a bunch of functions each check an error code and return to their caller?
Re:Practical Guide to Bare Metal C++ by monocasa on Friday June 02, 2017 @06:03PM (#14473640)
The practicalities of bare metal programming means that a lot of the time an individual work item has to jump contexts (ie. stacks and register sets) in a way that doesn't map cleanly to C++ style exceptions.
Re:Practical Guide to Bare Metal C++ by asveikau on Friday June 02, 2017 @05:00PM (#14473268)
If someone writes this ...

> I haven't found many practical guides or tutorials of how to use C++ superiority and boost development process compared to conventional approach of using “C” programming language.

... and does not mention RAII in their quest to write such a guide, they have seriously failed at their goal.