Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

errno

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?

like image 378
Erik Öjebo Avatar asked Jan 16 '09 07:01

Erik Öjebo


People also ask

Is errno thread safe?

Yes, it is thread safe. On Linux, the global errno variable is thread-specific. POSIX requires that errno be threadsafe.

Is write () thread safe?

write() is certainly thread-safe. The problem is that a partial write() could require multiple calls in order to completely write the data, and while that is "thread-safe" it could result in interleaved data.


1 Answers

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.

like image 75
Greg Hewgill Avatar answered Oct 01 '22 18:10

Greg Hewgill