Maxim Vexler <[EMAIL PROTECTED]> writes:
> Hello to everyone,
>
> After a recent question regarding libbzip2 on this list has send me to
> the source code, i've stumbled upon this comment : "The low-level part
> of the library has no global variables and is therefore thread-safe."
> on the documentation page :
> http://www.ugcs.caltech.edu/info/bzip2/manual_3.html#SEC14
>
> What I would like to know is why.
Thread-safe functions (and their children) should not hold any static
data, return pointers to static data, *or use unprotected shared
resources* (such as globals) etc.
Global variables by definition can be accessed from different threads
and thus should be protected ("serialized").
It is *not* enough to avoid global data to make your code thread-safe,
so the comment you quote looks shaky to me.
> Doesn't the kernel allocate each thread his own memory space
No, that's the benefit (and curse) of threads compared to processes.
> (including for var's which are global for that thread)?
> Should i take extreme measures to avoid using globals in my code ?
I wouldn't say, "extreme", but I suggest you carefully rationalize to
yourself why you need every piece of global data.
--
Oleg Goldshmidt | [EMAIL PROTECTED]
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]