有没有在多线程应用程序安全地使用错误号的方法吗?

问题描述:

如果你正在写一个使用的系统/库调用,使errno的使用,以指示错误类型,有没有使用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?

如果你的标准库是多线程的认识,那么它可能有一个的#define 的更改错误号成一个函数调用返回的线程局部错误返回值。但是,要使用这个你通常的必须的包括< errno.h中> ,而不是依靠一个 EXTERN 声明。

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.

我发现它解决过这个问题的文章线程安全性和POSIX.1

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