2 ms·
setenv() is indeed thread-safe. getenv() is not. That is, you can end up in the following situation: 1. thread1 calls getenv() and gets a pointer to a sectio
by kelnos 10y ago
setenv() is indeed thread-safe. getenv() is not. That is, you can end up in the following situation:
1. thread1 calls getenv() and gets a pointer to a section of environ A
2. thread2 calls setenv() & acquires lock & allocs environ B & frees environ A & releases lock
3. thread1 tries to look at the pointer in environ A, and crashes because environ A has been freed
Now, if you never call getenv(), and just call setenv() from multiple threads, that's safe, because setenv() itself is thread-safe. It's the combination that kills you.