Connecting Tech Pros Worldwide Forums | Help | Site Map

static variable thread safe?

Kumar
Guest
 
Posts: n/a
#1: Nov 14 '05
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

Peter Pichler
Guest
 
Posts: n/a
#2: Nov 14 '05

re: static variable thread safe?


"Kumar" <kumar_gaurava@yahoo.co.uk> wrote in message
news:80965d4.0401100252.36997e2b@posting.google.co m...[color=blue]
> 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...?[/color]

Yes.

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

Peter


Peter Pichler
Guest
 
Posts: n/a
#3: Nov 14 '05

re: static variable thread safe?


I wrote:[color=blue]
> "Kumar" <kumar_gaurava@yahoo.co.uk> wrote:[color=green]
> > 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...?[/color]
>
> Yes.[/color]

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


pete
Guest
 
Posts: n/a
#4: Nov 14 '05

re: static variable thread safe?


Peter Pichler wrote:[color=blue]
>
> "Kumar" <kumar_gaurava@yahoo.co.uk> wrote in message
> news:80965d4.0401100252.36997e2b@posting.google.co m...[color=green]
> > 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...?[/color]
>
> Yes.
>
> (The fact that threads, DLLs and similar stuff is off-topical on this
> group is irrelevant ;-))[/color]

Unless the right answer is really "No".

--
pete
Peter Pichler
Guest
 
Posts: n/a
#5: Nov 14 '05

re: static variable thread safe?


"pete" <pfiland@mindspring.com> wrote in message
news:4000001D.7A0@mindspring.com...[color=blue]
> Peter Pichler wrote:[color=green]
> >
> > "Kumar" <kumar_gaurava@yahoo.co.uk> wrote in message
> > news:80965d4.0401100252.36997e2b@posting.google.co m...[color=darkred]
> > > 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...?[/color]
> >
> > Yes.
> >
> > (The fact that threads, DLLs and similar stuff is off-topical on this
> > group is irrelevant ;-))[/color]
>
> Unless the right answer is really "No".[/color]

It depends on the question ;-)


pete
Guest
 
Posts: n/a
#6: Nov 14 '05

re: static variable thread safe?


Peter Pichler wrote:[color=blue]
>
> "pete" <pfiland@mindspring.com> wrote in message
> news:4000001D.7A0@mindspring.com...[color=green]
> > Peter Pichler wrote:[color=darkred]
> > >
> > > "Kumar" <kumar_gaurava@yahoo.co.uk> wrote in message
> > > news:80965d4.0401100252.36997e2b@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 ;-))[/color]
> >
> > Unless the right answer is really "No".[/color]
>
> It depends on the question ;-)[/color]

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
CBFalconer
Guest
 
Posts: n/a
#7: Nov 14 '05

re: static variable thread safe?


Kumar wrote:[color=blue]
>
> 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...?[/color]

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

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

Closed Thread