マルチスレッドアプリケーションでerrnoを安全に使用する方法はありますか? [複製]
質問
この質問にはすでに回答があります:
- errnoはスレッドセーフですか 8つの答え
errnoを使用してエラータイプを示すシステム/ライブラリ呼び出しを使用するマルチスレッドアプリケーションを作成している場合、errnoを使用する安全な方法はありますか?そうでない場合、エラーが発生したというだけでなく、発生したエラーのタイプを示す他の方法はありますか?
解決
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.
他のヒント
man errno
says:
errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread.