----- Ursprüngliche Nachricht -----
Von: Todd Allen
Gesendet am: 20 Jun 2009 16:11:02

> I finally tracked down an intermittent bug I was having -- strings
> constructed via snprintf() were being corrupted.  The problem turned out to
> be that I was also calling snprintf() within an interrupt service routine
> that was executed once per second.  snprintf() calls vsnprintf(), which
> stashes a pointer and a length in external variables.  So of course when an
> interrupt which calls snprintf() happens to land in the middle of a call to
> snprintf(), the string gets corrupted.

> Is it, in general, a bad idea to call library routines from ISRs, or is this
> an isolated case?

Hi Todd,

It's generally a bad idea to call library (or own) functions in ISRs which are 
not expilcitely declared thread-safe.
Also one should avoid functions which require a lot of stack space and/or time.

ISRs should be short or even shorter :)

printf and all of its derivates requires a lot of stack space (maybe causing 
intermittent and almost untraceable stack overflows), much time and they are 
not thread safe. Neither internally (due to some statically assigned 
buffers), nor in the result, as at least printf does not print its result in a 
whole but char after char as it is calculated, causing a printf inside an ISR 
to print inside the output of a printf from the main loop.

JMGross

btw: dividing values in an ISR should be avoided as well. It takes an awful lot 
of CPU time.


Reply via email to