4 ms·
Come on! Of all C/Posix thread-safety issues in circulation this can plausibly be considered the most moot one. Environment variables are not meant to be an in
by yason 3y ago
Come on! Of all C/Posix thread-safety issues in circulation this can plausibly be considered the most moot one.
Environment variables are not meant to be an inter-thread communication channel and the documentation that points out setenv() is not thread-safe is very much a fair shot.
You rarely, if ever, need to setenv() anything maybe unless you're a shell. For spawning children execve() already takes an envp parameter. For debugging I think I've mostly set in-process environment variables manually from gdb.
Further, because environment variables are an interface between the process and its environment you typically read environment variables at start and cache the parsed values in some internal location. If you need to change that global state on the go you should do it using your own internal variables instead of recycling it through the environment and having the program threads repeatedly getenv() the updated values.