• Complain

Arkady Miasnikov - C++ for embedded systems

Here you can read online Arkady Miasnikov - C++ for embedded systems full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2015, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Arkady Miasnikov C++ for embedded systems

C++ for embedded systems: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "C++ for embedded systems" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

C++s support for object-oriented programming, rich set of tools,its C roots make it attractive for embedded projects. A C developer can worry about code bloat and hidden performance costs. This book makes an attempt to demonstrate how C++ can improve the embedded software and in some cases accomplish things which are next to impossible in C. The book addresses issues such as code bloat and the hidden performance costs of C++, and demonstrates some of the lesser known features of the C++, such as type traits, static assertion, constant expressions. The last chapter of the book contains about 50 C/C++ interview questions.
Any questions? Do not hesitate to contact me arkady.miasnikov@gmail.com . I will love to hear what you think.

Arkady Miasnikov: author's other books


Who wrote C++ for embedded systems? Find out the surname, the name of the author of the book and a list of all author's works by series.

C++ for embedded systems — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "C++ for embedded systems" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

Table of Contents Introduction Ive come to the conclusion that any - photo 1 Table of Contents

Introduction.
I've come to the conclusion that any programmer that would prefer the project to be in C++ over C is likely a programmer that I really would prefer to piss off, so that he doesn't come and screw up any project I'm involved with. Linus Torvalds This book is intended for firmware developers who mainly use the C language. I assume that the reader is comfortable with ARM or Intel assembly language and has working knowledge of the C++ syntax. In this book, I have tried to give examples of C++ code in situations where, arguably, C is not the perfect tool for the task. Many C++ code examples come with snippets of the resulting assembly. The examples of code have been constructed in a way that allows immediate reuse in real-world applications.

Examples cover topics such as memory and speed optimization, organizing arrays, FIFOs, thread safety, and direct access to the hardware. The book addresses issues such as code bloat and the hidden performance costs of C++. In all examples I assume that the C++ compiler supports version 11 of the language. One example of such a compiler is GNUC 4.8. The availability of a C++ compiler supporting version 11 for specific hardware is not required, and most code examples can be rewritten for older C++ compilers. Throughout the book, I have demonstrated some of the lesser known features of the C++11 standard, such as type traits, static assertion, constant expressions and OpenMP support.

Problem can be solved in numerous ways. Where it was possible and made sense, I provided an alternative implementation in C and compared the performance of the C and C++ solutions.

Acknowledgements.
I would like to thank my cousin and friend Andre Bar'yudin. Without Andre's help and contributions this book would have been much poorer. I think that between two of us this is Andre who knows C++.
C++ code style.
#define AND && #define OR || #define EQ == I have done my best to follow consistent C/C++ code style in all source code examples.

In this book I have not placed curly brackets on a separate line. Please, do not send me hate mail. The only reason is to make the code snippets shorter. Shorter code has a better chance of fitting in the smaller e-reader displays. There are no comments in the code itself for the same reason. Hopefully, the lack of comments has been compensated for by the interspersed explanations.

I am using the camelcase name convention. Types and class names begin with uppercase and variables begin with lower case, while constants are all upper case with the underscore delimiter.

Software primitives.
Mutual exclusion.
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones. In the multi-task environment, threads and interrupts can concurrently read and write data objects. I can use different tools to synchronize access to the data between different contexts and create thread safe APIs. Among the available tools are semaphores and mutual exclusion APIs provided by operating systems, as well as disabling all interrupts, disabling some interrupts, disabling the operating system scheduler, and using spin locks.

When I write a wrapper around an API provided by my real-time operating system or by the hardware, I want the wrapper to be as thin as possible. Usually, I measure the overhead of the wrapper using a number of assembly instructions. The number of instructions provides a good approximation of the CPU cycles or execution time. I am starting with a code snippet that creates and calls a dummy lock object. class LockDummy { public : LockDummy() { cout << "Locked context" << endl; } ~LockDummy() { cout << "Lock is freed" << endl; } }; In the following usage example the C++ specifier auto tells to the compiler to automatically supply the correct type the C++11 compiler knows to deduct certain types of variables in some situations. For example, the C++ compiler can figure out the return type of the left side of an assignment or the return type of a function.

In the function main() compiler will call the output function two times and will not add any other code. The lock is released the destructor ~LockDummy gets called when the scope of the variable myDummyLock ends. The scope could be a while loop. I do not have to call unlock before each and every return from the function. The C++ compiler makes sure that the destructor is always called and called exactly once (see more about RAII in [1]). This convenient service comes without any performance overhead.

The output of the following code is going to be Locked context Protected context Lock is freed int testDummyLock () { #if (__cplusplus >= 201103) // use "auto" if C++11 or better auto myDummyLock = LockDummySimple (); #else LockDummySimple myDummyLock = LockDummySimple(); #endif cout << "Protected context" << endl ; return 0; } I want to stress this idea using another example. In the following code, I set a scope around the declaration of the lock variable. The output of the code is going to be Locked context Protected context Lock is freed End of main int main() { { LockDummy lock; cout << "Protected context" << endl ; } cout << "End of main" << endl; return 0; } My second lock is a more realistic one, and it disables a hardware interrupt. I assume that there is an API that disables and enables an interrupt. I am refactoring the original lock code a little bit. First of all, I use a template, which is a feature of C++ that allows you to declare a class that operates with generic types.

I am going to reuse the code in the template Lock for different synchronization objects. The code in the template Lock can manipulate any structure or class that implements two public methods: static methods get() and release(). I carefully avoid polymorphism here, I do not require synchronization objects to belong to the same hierarchy of classes. The wrapper around the disable/enable interrupts API, which likely writes to the hardware, is not going to be a child/friend/parent/relative/derivation of a wrapper for the operating system semaphore. This is what the API that disables and enables interrupts looks like: static inline void interruptDisable ( void ) { cout << "Disable" << endl ; } static inline void interruptEnable ( void ) { cout << "Enable" << endl ; } I am describing a synchronization object. The class implements two methods: get and release.

Both methods are inline, which helps the optimizer to decide if calls to the methods should be substituted by the methods code. The end result is going to be similar to using a macro definition in C. The default constructor in the class SynchroObject is private, I do not want any objects of this type in the application. My C++ compiler so far has not added any object code to my executable file. class SynchroObject { SynchroObject () {}; public : static inline void get () { interruptDisable(); } static inline void release () { interruptEnable(); } }; The template class Lock can manipulate any type of synchronization objects that provides a get/release API. All methods of the template class Lock are inline.

The C++ compiler is not going to add any functions to the object code, but will rather replace the calls to the Lock methods with the code of the get/release from the synchronization object. template < typename Mutex > class Lock { public : inline Lock () { Mutex ::get(); } inline ~Lock () { Mutex ::release(); } }; Declare a new type MyLock that uses my SynchroObject to disable/enable interrupts. There is still no additional data or code in my executable except for the calls to the interrupt enable and interrupt disable API. typedef Lock < SynchroObject > MyLock ; Output of the function main() is going to be two words: Disable, Enable int main () { { MyLock lock ; } return 0; } Overhead of the wrapper around the functions that disable and enable interrupts is exactly zero. Indeed, if I check the disassembly, I will see two calls to the print function in the main routine and nothing else. I have written some C++ code which gets optimized to nothing.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «C++ for embedded systems»

Look at similar books to C++ for embedded systems. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «C++ for embedded systems»

Discussion, reviews of the book C++ for embedded systems and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.