Take the 2-minute tour ×

I'm trying to understand how glibc initializes errno without the preprocessor substituting the errno symbol.

I first tried to implement a simple version myself based on csu/errno-loc.c and csu/errno.c:


#ifndef MYERRNO_H
#define MYERRNO_H

extern int *myerrno_location(void);
#define myerrno (*myerrno_location())



#include "myerrno.h"

static int myerrno = 0;

int *myerrno_location(void){
    return &myerrno;

However, when I try to compile I receive the following error messages:

myerrno.c:3:1: error: function ‘myerrno_location’ is initialized like a variable
myerrno.c:3:12: error: static declaration of ‘myerrno_location’ follows non-static declaration
myerrno.h:4:13: note: previous declaration of ‘myerrno_location’ was here

I can tell that the preprocessor is substituting (*myerrno_location(void)) when it encounters myerrno on line 3 -- and naturally this is expected behavior.

I don't understand why this isn't a problem for glibc. How do thread-safe implementations of errno get around this preprocessor substitution issue without renaming the static errno variable?

share|improve this question
is this thread helpful ? stackoverflow.com/questions/1694164/is-errno-thread-safe –  DRC Aug 2 '13 at 21:06
In threaded code, there isn't a single location for an errno variable; there is one variable per thread, and the function hidden behind the macro is responsible for returning a pointer to the relevant thread-specific variable. –  Jonathan Leffler Aug 2 '13 at 21:13
@JonathanLeffler Are static variables are shared between threads? –  Vilhelm Gray Aug 2 '13 at 21:26
Yes (static variables in general are shared between threads). But in a threaded environment, the memory location designated by errno is no longer (simply) a static variable; it becomes a thread-local variable — each thread is given its own memory location which 'is' that thread's 'errno'. –  Jonathan Leffler Aug 2 '13 at 21:30
C99 doesn't know what a thread is — you'd have to refer to POSIX to find the definition. C11 does make promises/requirements (ISO/IEC 9899:2011 §7.5 Errors <errno.h>: errno which expands to a modifiable lvalue(201) that has type int and thread local storage duration, the value of which is set to a positive error number by several library functions. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined. –  Jonathan Leffler Aug 5 '13 at 13:59
show 2 more comments

1 Answer

up vote 3 down vote accepted

Fixing your issue is as easy as changing the name of your static variable.

static int myerrno_variable = 0;

int *myerrno_location(void){
    return &myerrno_variable;

Notice that your version is still not thread safe since all threads are accessing the same myerrno_variable. A real implementation would return a thread specific memory location. In GCC, there is an extension that provides the __thread storage class. C.11 provides its own version of that called thread_local, but it is only available if thread support is provided by the implementation (which can be checked by looking if __STDC_NO_THREADS__ is defined or not).

static __thread int myerrno_variable_gcc;      /* if using GCC */
static thread_local int my_errno_variable_c11; /* if __STD_NO_THREADS__ isn't defined */

On a POSIX system without a thread local feature, an implementation could use pthread_getspecific() to get a pointer to thread specific data that was allocated for each thread, and set with pthread_setspecific(). See the manual for more information.

share|improve this answer
So glibc is able to use the same name since it uses the GCC extension __thread, while most other implementations simply rename the variable? –  Vilhelm Gray Aug 2 '13 at 21:24
@VilhelmGray: Most implementations probably have a similar extension, since the feature has been added to the C.11 standard. However, even if it were not the case, POSIX still provides a mechanism to allocate thread specific storage. –  jxh Aug 2 '13 at 21:26
@VilhelmGray: Oh, glibc probably fixes it the same way I told you to fix it. It wouldn't use the same name for the variable and the macro like that. The __thread extension allows each thread to have their own private variable that the thread can refer to by the same name. –  jxh Aug 2 '13 at 21:36
You misunderstood. @JonathanLeffler was telling you how errno specifically behaves in a multi-threaded environment, not how static variables behave in general. The C library implementation has to achieve that behavior. One way is for the implementation to use __thread or some equivalent feature. –  jxh Aug 5 '13 at 18:55
If the library implementation declares errno with __thread, there is no need for a macro. The macro version of errno which actually calls a function predates the presence of a __thread feature. –  jxh Aug 5 '13 at 20:07
show 5 more comments

Your Answer


