• AVR Freaks

Hot!are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ?

Author
AlexGiov_uc
Starting Member
  • Total Posts : 41
  • Reward points : 0
  • Joined: 2012/12/10 10:39:21
  • Location: 0
  • Status: offline
2019/08/11 07:48:03 (permalink)
0

are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ?

Hello,
 
I'm using FreeRTOS official demo for PIC32MX in MPLAB X and XC32 compiler.
Are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ?
 
#1

4 Replies Related Threads

    Larry.Standage
    Moderator
    • Total Posts : 908
    • Reward points : 0
    • Joined: 2011/12/30 09:50:47
    • Location: 0
    • Status: offline
    Re: are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ? 2019/08/12 09:07:41 (permalink)
    +2 (2)
    Unless FreeRTOS has their own versions of those functions that they specify as thread-safe, the best assumption is that they're not.
    #2
    moser
    Super Member
    • Total Posts : 504
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ? 2019/08/15 03:13:23 (permalink)
    0
    Actually, it would be good if Microchip could provide in the "32-Bit Language Tools Libraries" document a complete list for all functions, and just state, if the function is thread- and interrupt-safe or not. I can imagine, that for some functions it might even depend on the chosen implementation (e.g. if you use the lib with HW floating point or SW floating point or such things).
     
    It is not important that all those functions are thread safe. It would be just nice to know. You don't always want to reinvent the wheel, although there is already an appropriate library function which is already thread safe.
     
    #3
    simong123
    Lab Member No. 003
    • Total Posts : 1309
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: offline
    Re: are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ? 2019/08/15 06:59:11 (permalink)
    0
    Traditionally functions such as str(n)cmp(), memcpy() et al are thread safe. Even the most naive implementations don't use more than the stack, and store no state between calls.
     
    Functions such as strtok(), which maintain internal state accross function calls are traditionally not thread safe.
     
    printf(), sprintf() etc. may or may not be thread safe. They (or their helper functions) use buffers to store intermediate values which may or may not be static (not safe) or on the stack/heap (safe), which is entirely implementation dependant.
     
    If you are using legacy libc (the default on later versions of the compiler) all the functions *should* be thread safe* (except strtok()) as they use the GNU libc library (I'm not sure which version for each compiler version) wihch is posix compliant. See the docmentation at www.man7.org and www.gnu.org for more detailed information than you will get in the MCHP documentation.
     
    *printf and the like may access e.g. locale information which may be changed asynchronously. As such this should be set up at startup and not changed thereafter.
    #4
    moser
    Super Member
    • Total Posts : 504
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: are sprintf snsprintf memcmp memcpy strncpy ecc... thread safe ? 2019/08/20 07:00:06 (permalink)
    +1 (1)
    Some more functions which are not thread safe, as they are returning pointers are:
    asctime(), ctime(), gmtime(), localtime()
     
    Thanks, for pointing to the GNU documentation, simong123. 
    https://www.gnu.org/software/libc/manual/html_node/POSIX.html together with the rest of https://www.gnu.org/software/libc/manual/html_node/POSIX.html gives some good hints. 
     
    However, I still think it would be good if Microchip would document which functions are thread safe or not, or just point to the mentioned documentation for those functions oder header files, which conform to standards.
    #5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5