Prev: Qt question
Next: Join Lines
From: param on
Is it advisable to use getenv and put/setenv functions in
multithreaded programs as these functions act on the global variable
__environ.?
I feel that there can be a chance for one thread change the environ
list location while another thread iterates through the list. example
T1 executing getenv & it had context switched after copied the environ
variable into loop variant and meanwhile T2 moved the environ list to
new location to accommodate the new env. variable. if T1 continues
the execution after T2, T1 might fail. i got this doubt because of
this condition, or is it valid condition?
From: Rainer Weikusat on
param <sparameswara(a)gmail.com> writes:
> Is it advisable to use getenv and put/setenv functions in
> multithreaded programs as these functions act on the global variable
> __environ.?

The environment is a per-process ressources and the interface
manipulating it are described as 'need not be
thread-safe'. Consequently, it would be prudent to explicitly
serialize environment operations in multi-threaded programs.
From: Geoff Clare on
param wrote:

> Is it advisable to use getenv and put/setenv functions in
> multithreaded programs as these functions act on the global variable
> __environ.?

No, it's not advisable. They are in the list in POSIX of functions
that are not required to be thread-safe. After the list it also
says this about use of environ directly:

"Since multi-threaded applications are not allowed to use the
environ variable to access or modify any environment variable
while any other thread is concurrently modifying any environment
variable, any function dependent on any environment variable is
not thread-safe if another thread is modifying the environment"

The Austin Group has agreed that getenv() will be required to be
thread-safe in the next POSIX revision; however the above note
will change to say "... the getenv() function and any function
dependent on any environment variable ...".

--
Geoff Clare <netnews(a)gclare.org.uk>


From: Scott Lurndal on
param <sparameswara(a)gmail.com> writes:
>Is it advisable to use getenv and put/setenv functions in
>multithreaded programs as these functions act on the global variable
>__environ.?
>I feel that there can be a chance for one thread change the environ
>list location while another thread iterates through the list. example
>T1 executing getenv & it had context switched after copied the environ
>variable into loop variant and meanwhile T2 moved the environ list to
>new location to accommodate the new env. variable. if T1 continues
>the execution after T2, T1 might fail. i got this doubt because of
>this condition, or is it valid condition?

POSIX doesn't require these interfaces to be thread-safe. You can
still use them, but you must serialize accesses between threads
with something like pthread_mutex_lock()/pthread_mutex_unlock().

scott
 | 
Pages: 1
Prev: Qt question
Next: Join Lines