This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to use errno in multithread environment?

Hi,

The traditional 'errno' global variable is not safe in multithreading environment. From errno.h, I figure out it should be possible to define the _Geterrno() function to use different errno variables for each thread. My question is: how can I do that?

Any suggestion is welcome, thanks.

  • Which compiler do you use?  What version?

    Thanks and regards,

    -George

  • Hi George:

    I am using TI C/C++ Compile for ARM. I also updated it to the last version in CCS.

  • The function _Geterrno was never really supported in the RTS library provided with the TI ARM compiler, and it has been removed in the latest version. I do not recommend you start using it now.

    The TI ARM compiler doesn't really support thread-local storage, although the ARM EABI does.

    The ARM EABI defines the function __aeabi_errno_addr, which does essentially the same thing. When compiling in EABI mode when _AEABI_PORTABILITY_LEVEL has a non-zero value, use errno as you normally would, and what you'll really get is a call to this function. However, in order to get a truly thread-local errno, you will need some additional support from the RTS and the linker. You'll have to recompile the TI RTS with _AEABI_PORTABILITY_LEVEL=1 so that functions which access errno will use __aeabi_errno_addr. I'm not sure whether you can use the TI ARM linker as-is, but I suspect you can. In any case, you would probably be better off using another vendor's linker.
  • Hi Archaeologist,

    Thanks for your kindly reply.

    It looks like it is better to not use errno at all. I will keep this in mind.

    rcfocus.