Project Home
Project Home
Source Code
Source Code
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
BroadcastCommunity.qnx.com will be offline from May 31 6:00pm until June 2 12:00AM for upcoming system upgrades. For more information please go to https://community.qnx.com/sf/discussion/do/listPosts/projects.bazaar/discussion.bazaar.topc28418
Forum Topic - Using C++ in a hard real-time system: (7 Items)
   
Using C++ in a hard real-time system  
A customer is considering using C++ in a Neutrino based hard real-time system.

He is asking if there are any C++ constructs that should be avioded in a hard real-time system?

Thanks,

Garry
Re: Using C++ in a hard real-time system  
I've used C++ in embedded systems before with good success.  The use of virtual or overridden functions is not a problem
, since they're just an additional pointer call in terms of penalty.    Probably the worst C++ issue for real-time is 
rampant use of "new/delete" in some code.  The allocator doesn't have a fixed-time execution.  

Even new/delete isn't necessarily bad because the time spent in the allocator is reasonably bounded (I suspect it's 
O(log N), but I don't know for sure).  I've got customers using a speech recognition engine that does several thousand 
new/deletes per second as it's combing the search tree matching the utterance to the grammar, and it still behaves 
deterministically.  If the customer's code isn't oversensitive to timing jitter, then they should be fine.  Even if they
 are writing a very timing sensitive section of code, the customer can write their own functions that pre-allocate any 
objects that they're using or exclusively use the stack.
Re: Using C++ in a hard real-time system  
We also use C++ for our data acquisition system, including some real-time instrument control loops.  Andy's pretty much 
said it all - there aren't any real gotchas other than keeping an eye out for implicit memory allocation on critical 
paths.
Re: Using C++ in a hard real-time system  
I wrote a paper that how shows C++ produces faster execution (in real-world practice) than 'C' does.  The basic reason 
for this, is that exceptions are correctly utilized; the non-error case (which is what matters for hard real-time) is 
far more efficient than the equivalent 'C' code (since the C++ code does not need to check error return conditions in 
the success path).

The key to getting optimal speed in C++ is to use exceptions (correctly of course) for all error handling.
Re: Using C++ in a hard real-time system  
> additional pointer call in terms of penalty.    Probably the worst C++ issue 
> for real-time is rampant use of "new/delete" in some code.  The allocator 
> doesn't have a fixed-time execution.  

Characterizing new/delete as a problem in C++ (when comparing to 'C' at least) is somewhat disingenuous, since C++ 
provides far better facilities to the developer than 'C' does.  Due to overloading, C++ allows a custom allocator/
deallocator to be transparently invoked on any new/delete.  A 'C' developer would need to create a cover function to 
handle this (no trivial feat if there is much legacy code base to deal with).

A well designed hard real-time system in C++ would have the static factory class pre-allocate instance aligned memory 
for all the instances that it is capable of manufacturing when the factory class is instantiated (i.e. at init time), 
and the factory class header would publish overloaded new/delete operators for each class (that would instantiate the 
objects into this pre-allocated, and aligned memory).

Alternatively, each class can provide its own new/delete, but must either do the "pre-allocation" on first instantiation
, or require that the client code do the pre-allocation, and provide an allocator callback (which is quite awkward).
Re: Using C++ in a hard real-time system  
> A customer is considering using C++ in a Neutrino based hard real-time system.
> 
> 
> He is asking if there are any C++ constructs that should be avioded in a hard 
> real-time system?

Go back 20 years ago and the question would be:  He is asking if there is any C function that should be avoided in a 
hard real-time system. Of course the person asking is programming in assembly code ;-0

> 
> Thanks,
> 
> Garry


Re: Using C++ in a hard real-time system  
> > A customer is considering using C++ in a Neutrino based hard real-time 
> system.
> > 
> > 
> > He is asking if there are any C++ constructs that should be avioded in a 
> hard 
> > real-time system?
> 
> Go back 20 years ago and the question would be:  He is asking if there is any 
> C function that should be avoided in a hard real-time system. Of course the 
> person asking is programming in assembly code ;-0
> 
> > 
> > Thanks,
> > 
> > Garry
> 
> 


To round this up with a little 'context free' humor see:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=5

It is ok as long as it works, isn't it ?

Jeevan