Is errno thread-safe?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On modern POSIX systems and mainstream C runtimes, errno is thread-safe in the sense that each thread gets its own logical errno value. That does not mean errno is magical or immune to misuse, though. You still need to read it immediately after the call that failed and avoid assuming it keeps its old value forever.
What errno Really Is
errno is not usually a single global integer shared by every thread. In modern implementations it is typically a macro that resolves to thread-local storage, so one thread's error state does not overwrite another's.
This matters because system calls and library functions often report failure like this:
If two threads call failing functions at the same time, each thread still sees its own errno.
Why It Is Considered Thread-Safe
Thread safety here means thread isolation, not that every possible use is automatically safe. The runtime ensures that when thread A sets errno, thread B does not see or overwrite that same storage location.
A small pthread example demonstrates the idea:
Both threads may hit an error, but each reads its own thread-local errno.
The Important Practical Rule
Even though errno is thread-local, you still have to capture it immediately after the failure you care about. Many other functions can modify errno, including helper functions you call while logging or formatting output.
This is the safe pattern:
Saving the value avoids surprises if later calls change it before you finish reporting the failure.
What Thread-Safe Does Not Mean
It does not mean:
- '
errnois reset to zero before every function call' - every failed function must set it in a way you expect
- it is safe to inspect
errnoif the function did not indicate failure
Many functions only promise meaningful errno values when they explicitly report failure. If a function succeeds, errno may still hold some older unrelated value.
Common Pitfalls
The biggest mistake is checking errno without checking the function's documented return value first. If the call succeeded, errno may contain stale data from a previous unrelated failure.
Another issue is calling other library functions before preserving the error code. Even though another thread will not overwrite your thread's errno, your own thread can overwrite it by making additional calls.
Developers also sometimes assume thread-safe means portable to every historical system ever built. In practice, modern POSIX environments behave correctly, but very old or unusual environments deserve verification.
Finally, note that strerror() itself has thread-safety caveats on some platforms. If you need stricter control, use the thread-safe variant available on your platform, such as strerror_r.
Summary
- '
errnois thread-safe on modern systems because it is effectively thread-local.' - One thread's error code does not overwrite another thread's
errno. - Read or save
errnoimmediately after the failing call you care about. - Never inspect
errnounless the function's return value indicates failure. - Thread-safe
errnostill requires careful usage patterns in your own code.
Related reading
- Is ExecutorService specifically ThreadPoolExecutor thread safe?
- Is function call an effective memory barrier for modern platforms?
- Is get_result a required call for put_async in Google App Engine
- Is HttpClient safe to use concurrently?
- Is id 1 - id atomic?
- Is incrementing an int effectively atomic in specific cases?
- Is it a reasonable trade-off to use `sleep` in an asynchronous job?
- Is it an anti-pattern to use async/await inside of a new Promise constructor?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.