Is there a way to use errno safely in a multi-threaded application?

Erik Öjebo picture Erik Öjebo · Jan 16, 2009 · Viewed 7.8k times · Source

If you are writing a multi-threaded application that uses system/library calls that make use of errno to indicate the error type, is there a safe way to use errno? If not, is there some other way to indicate the type of error that occurred rather than just that an error has occurred?

Answer

Greg Hewgill picture Greg Hewgill · Jan 16, 2009

If your standard library is multithread aware, then it probably has a #define that changes errno into a function call that returns a thread-local error return value. However, to use this you generally must include <errno.h>, rather than relying on an extern declaration.

I found an article Thread-safety and POSIX.1 which addresses this very question.