Reply by FreeRTOS.org December 22, 20082008-12-22
"Not Really Me" <scott@validatedQWERTYsoftware.XYZZY.com> wrote in message 
news:6r9q9gF5k0vU1@mid.individual.net...
> JeGy wrote: >> Hi, >> >> does anyone here know >> if the IAR AVR C-compiler sprintf() and associated functions >> are thread safe in in multi-tasking, e.i. FreeRTOS? >> >> Regards >> >> Jens > > Sorry I didn't think of checking this when you first posted. I checked > the source, none of the printf variants in the IAR libraries are thread > safe, this includes sprintf.
According to their AVR compiler documentation their sprintf() implementation is thread safe - so it is interesting if your inspection shows otherwise. The FreeRTOS download includes a mini implementation of sprintf() that is thread safe. I normally use that regardless as it uses much less stack than standard library variants. -- Regards, Richard. + http://www.FreeRTOS.org Designed for microcontrollers. More than 7000 downloads per month. + http://www.SafeRTOS.com Certified by T&#4294967295;V as meeting the requirements for safety related systems.
Reply by Not Really Me December 22, 20082008-12-22
JeGy wrote:
> Hi, > > does anyone here know > if the IAR AVR C-compiler sprintf() and associated functions > are thread safe in in multi-tasking, e.i. FreeRTOS? > > Regards > > Jens
Sorry I didn't think of checking this when you first posted. I checked the source, none of the printf variants in the IAR libraries are thread safe, this includes sprintf. -- Scott
Reply by JeGy December 20, 20082008-12-20
Hi everybody,


Thanks for your answers and comments, is is good to know that one have 
friends out there when problems arise.

I am sorry that the subject was misleading, and I was not aware of the 
significant differences between sprintf and printf.

However, I have made my own thread safe sprintf function, and the embedded 
software now runs perfect.

So the conclusion is, that in this specific case, the sprintf was not thread 
safe

Best regards and a Happy Christmas

Jens

"JeGy" <gydesen.jensREMOVE@THISgmail.com> skrev i en meddelelse 
news:494a786d$0$15873$edfadb0f@dtext01.news.tele.dk...
> Hi, > > does anyone here know > if the IAR AVR C-compiler sprintf() and associated functions > are thread safe in in multi-tasking, e.i. FreeRTOS? > > Regards > > Jens >
Reply by John Devereux December 19, 20082008-12-19
bastian42@yahoo.com (42Bastian Schick) writes:

> On Fri, 19 Dec 2008 09:50:58 PST, > mojaveg@mojaveg.lsan.mdsg-pacwest.com (Everett M. Greene) wrote: >
[...]
> > I don't think fprintf() is any better than printf(). > Actually the printf()/fprintf() function itself are maybe threadsafe > (neglecting the errno), but the underlying I/O is not thread-safe with > respect to single characters. > > I haven't seen a (f)printf() implementation which uses static > variables, so the function is thread-safe.
There are implementations of floating point math that are not thread-safe... so there must certainly also be printf ones too! -- John Devereux
Reply by 42Bastian Schick December 19, 20082008-12-19
On Fri, 19 Dec 2008 09:50:58 PST,
mojaveg@mojaveg.lsan.mdsg-pacwest.com (Everett M. Greene) wrote:

>Tim Wescott <tim@seemywebsite.com> writes: >> On Thu, 18 Dec 2008 17:21:07 +0100, JeGy wrote: >> >> > does anyone here know >> > if the IAR AVR C-compiler sprintf() and associated functions are thread >> > safe in in multi-tasking, e.i. FreeRTOS? >> > >> > Regards >> > >> > Jens >> >> At a top level, printf isn't really thread safe, at least to the extent >> that you cannot use printf in a lot of otherwise acceptable ways that >> don't end up with mangled printout. Why? Because if thread A prints out >> "How do I love you?", then thread B prints out "Toilet Clogged!!\n", then >> thread A prints out " Let me count the ways.\n", each thread will be >> using printf "acceptably", yet the output will be puzzling, at best. >> >> So were I developing a library, I wouldn't have a great deal of >> motivation to make printf thread safe (fprintf, OTOH, should be thread >> safe for any individual file). > >How is it that one variant of printf() can be "thread-safe" >when another isn't? They're all the same except for the >destination of the result.
I don't think fprintf() is any better than printf(). Actually the printf()/fprintf() function itself are maybe threadsafe (neglecting the errno), but the underlying I/O is not thread-safe with respect to single characters. I haven't seen a (f)printf() implementation which uses static variables, so the function is thread-safe. -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
Reply by Vladimir Vassilevsky December 19, 20082008-12-19

Tim Wescott wrote:

> I'm not saying that it would be hard to _do_; my point is that printf is > designed with the underlying assumption that there is one data source > that is printing to one data sink, and so there is nothing in the 'normal > usage' of printf that makes its usage inherently thread safe. If you > want to print messages to a common output (such as logging or what not), > then you need to package those messages up as whole entities and send > them, so they don't get scrambed up with one another. Printf is simply > not suited to provide that functionality.
Unless stated explicitly, *.printf functions should not be assumed to be thread safe. Reason: format conversions and i/o errors modify the global variables such as errno. If the local thread storage is supported in the stddio library and by the OS, then *.printf functions are safe. However it is not a big deal to protect *.printf by a semaphore anyway. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Reply by Everett M. Greene December 19, 20082008-12-19
Tim Wescott <tim@seemywebsite.com> writes:
> On Thu, 18 Dec 2008 17:21:07 +0100, JeGy wrote: > > > does anyone here know > > if the IAR AVR C-compiler sprintf() and associated functions are thread > > safe in in multi-tasking, e.i. FreeRTOS? > > > > Regards > > > > Jens > > At a top level, printf isn't really thread safe, at least to the extent > that you cannot use printf in a lot of otherwise acceptable ways that > don't end up with mangled printout. Why? Because if thread A prints out > "How do I love you?", then thread B prints out "Toilet Clogged!!\n", then > thread A prints out " Let me count the ways.\n", each thread will be > using printf "acceptably", yet the output will be puzzling, at best. > > So were I developing a library, I wouldn't have a great deal of > motivation to make printf thread safe (fprintf, OTOH, should be thread > safe for any individual file).
How is it that one variant of printf() can be "thread-safe" when another isn't? They're all the same except for the destination of the result.
Reply by Tim Wescott December 19, 20082008-12-19
On Fri, 19 Dec 2008 03:36:27 -0800, vladitx wrote:

> The OP asked about "sprintf()", although the thread subject is > misleading.
I was distracted by the title, and missed that essential point. You would certainly want sprintf to be thread safe!
> If that's the case, "sprintf()" may well be thread-safe according to the > answer of Richard. > > Thread-safe "printf()" is not hard to do with the usual ring buffer, and > with enough buffer size the calls won't be blocking more than the > formatting time. But for AVR this is overkill.
I'm not saying that it would be hard to _do_; my point is that printf is designed with the underlying assumption that there is one data source that is printing to one data sink, and so there is nothing in the 'normal usage' of printf that makes its usage inherently thread safe. If you want to print messages to a common output (such as logging or what not), then you need to package those messages up as whole entities and send them, so they don't get scrambed up with one another. Printf is simply not suited to provide that functionality. -- Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
Reply by vladitx December 19, 20082008-12-19
The OP asked about "sprintf()", although the thread subject is
misleading.

If that's the case, "sprintf()" may well be thread-safe according to
the answer of Richard.

Thread-safe "printf()" is not hard to do with the usual ring buffer,
and with enough buffer size the calls won't be blocking more than the
formatting time. But for AVR this is overkill.
Reply by Tim Wescott December 19, 20082008-12-19
On Thu, 18 Dec 2008 17:21:07 +0100, JeGy wrote:

> Hi, > > does anyone here know > if the IAR AVR C-compiler sprintf() and associated functions are thread > safe in in multi-tasking, e.i. FreeRTOS? > > Regards > > Jens
At a top level, printf isn't really thread safe, at least to the extent that you cannot use printf in a lot of otherwise acceptable ways that don't end up with mangled printout. Why? Because if thread A prints out "How do I love you?", then thread B prints out "Toilet Clogged!!\n", then thread A prints out " Let me count the ways.\n", each thread will be using printf "acceptably", yet the output will be puzzling, at best. So were I developing a library, I wouldn't have a great deal of motivation to make printf thread safe (fprintf, OTOH, should be thread safe for any individual file). -- Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html