在POSIX.1-2017中:
getenv()函数不必是线程安全的。
但是,在手册页中,genenv是MT Safe env。
│Interface │ Attribute │ Value │
│getenv(), secure_getenv() │ Thread safety │ MT-Safe env │
然而,
getenv()的实现不需要重入。getenv()返回值指向的字符串可以静态分配,并且可以通过随后对getenv()、putenv(3)、setenv(3)或unsetenv(3)的调用进行修改。
那么,什么是
MT-safe env
??谢谢您!
最佳答案
我从man 7 attributes中得到答案。
env Functions marked with env as an MT-Safety issue access the
environment with getenv(3) or similar, without any guards to
ensure safety in the presence of concurrent modifications.
We do not mark these functions as MT-Unsafe, however, because
functions that modify the environment are all marked with
const:env and regarded as unsafe. Being unsafe, the latter
are not to be called when multiple threads are running or
asynchronous signals are enabled, and so the environment can
be considered effectively constant in these contexts, which
makes the former safe.
谢谢shawn
关于linux - 为什么getenv MT安全手册页中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57541361/