By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

static variable thread safe?

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Kumar" <ku***********@yahoo.co.uk> wrote in message
news:80*************************@posting.google.co m...
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
Nov 14 '05 #2

P: n/a
I wrote:
"Kumar" <ku***********@yahoo.co.uk> wrote:
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.


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
Nov 14 '05 #3

P: n/a
Peter Pichler wrote:

"Kumar" <ku***********@yahoo.co.uk> wrote in message
news:80*************************@posting.google.co m...
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 ;-))


Unless the right answer is really "No".

--
pete
Nov 14 '05 #4

P: n/a
"pete" <pf*****@mindspring.com> wrote in message
news:40**********@mindspring.com...
Peter Pichler wrote:

"Kumar" <ku***********@yahoo.co.uk> wrote in message
news:80*************************@posting.google.co m...
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 ;-))


Unless the right answer is really "No".


It depends on the question ;-)
Nov 14 '05 #5

P: n/a
Peter Pichler wrote:

"pete" <pf*****@mindspring.com> wrote in message
news:40**********@mindspring.com...
Peter Pichler wrote:

"Kumar" <ku***********@yahoo.co.uk> wrote in message
news:80*************************@posting.google.co m...
> 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 ;-))


Unless the right answer is really "No".


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}.

--
pete
Nov 14 '05 #6

P: n/a
Kumar wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.