static variable thread safe?

K

Kumar

Hi,

If i am using static variable in a function, in say a dll. If that dll
is being used by a multi-threaded program, will the static variable be
safe to use? I mean, will it have the problems associated with using
global variables in this case, i.e. same variable being modified by
multiple threads, and hence creating issues...?

Thanks,
Kumar
 
P

Peter Pichler

Kumar said:
If i am using static variable in a function, in say a dll. If that dll
is being used by a multi-threaded program, will the static variable be
safe to use? I mean, will it have the problems associated with using
global variables in this case, i.e. same variable being modified by
multiple threads, and hence creating issues...?

Yes.

(The fact that threads, DLLs and similar stuff is off-topical on this
group is irrelevant ;-))

Peter
 
P

Peter Pichler

I said:

On the second thought, I may not have been as clear as I could have been.

The answer is "no" to your first question and "yes" to the other(s).

Peter
 
P

pete

Peter said:
It depends on the question ;-)

Anyway, you can convert a function from one that uses a
local static variable, to one that doesn't,
simply by replacing the local static variable
with a parameter which points to an
{automatic variable in the calling function}.
 
C

CBFalconer

Kumar said:
If i am using static variable in a function, in say a dll. If that
dll is being used by a multi-threaded program, will the static
variable be safe to use? I mean, will it have the problems
associated with using global variables in this case, i.e. same
variable being modified by multiple threads, and hence creating
issues...?

In a word, yes. A static variable is always a 'global', but with
restricted visibility.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top