471,326 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Static variables thead safe?

Hi,

Are static variables thread safe? I have a static byte array. This will be
accessed from more than one thread. Is it automatically thread safe by being
static rather than an instance or do i need to write code to make it thread
safe?

Cheers
Macca
Feb 14 '06 #1
6 2354
No variable is automatically thread-safe, not even static.

"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com...
Hi,

Are static variables thread safe? I have a static byte array. This will be
accessed from more than one thread. Is it automatically thread safe by
being
static rather than an instance or do i need to write code to make it
thread
safe?

Cheers
Macca

Feb 14 '06 #2

"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com...
| Hi,
|
| Are static variables thread safe? I have a static byte array. This will be
| accessed from more than one thread. Is it automatically thread safe by
being
| static rather than an instance or do i need to write code to make it
thread
| safe?
|
| Cheers
| Macca

No, certainly not a static variable, only local variables are by definition
thread safe (for as long as they stay 'local').

Willy.
Feb 14 '06 #3
Macca <Ma***@discussions.microsoft.com> wrote:
Are static variables thread safe? I have a static byte array. This will be
accessed from more than one thread. Is it automatically thread safe by being
static rather than an instance or do i need to write code to make it thread
safe?


I mentioned this a couple of days ago, but...

If you have thread local static variables (i.e. static variables with
the ThreadLocalAttribute applied), they're thread-safe by definition.
Of course, if they have values which are references to objects which
are used in multiple threads, then manipulating those objects needs to
be made thread-safe, but the variables themselves are thread-safe.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '06 #4
If you have thread local static variables (i.e. static variables with
the ThreadLocalAttribute applied), they're thread-safe by definition.
Of course, if they have values which are references to objects which
are used in multiple threads, then manipulating those objects needs to
be made thread-safe, but the variables themselves are thread-safe.


I don't know what ThreadLocalAttribute is. There is no information on such
an attribute in the docs (or at least I can't find it).
If this is what I think it is - to create thread bound data using thread's
data slots, I wouldn't say that static variables marked with
ThreadLocalAttribute are thread safe. As I understand thread safety is that
more than one thread can manipulate the variable without collision. If the
data is thread bound that means it exist only for sepcific thread. In this
case no other thread can use that data. This is no thread safety because
only one thread can use the data.

My answer to the question is - No, static variables by themselves are not
thread safe.
--
HTH
Stoitcho Goutsev (100)
Feb 14 '06 #5
Stoitcho Goutsev (100) <10*@100.com> wrote:
If you have thread local static variables (i.e. static variables with
the ThreadLocalAttribute applied), they're thread-safe by definition.
Of course, if they have values which are references to objects which
are used in multiple threads, then manipulating those objects needs to
be made thread-safe, but the variables themselves are thread-safe.
I don't know what ThreadLocalAttribute is. There is no information on such
an attribute in the docs (or at least I can't find it).


Oops, sorry - ThreadStaticAttribute.
If this is what I think it is - to create thread bound data using thread's
data slots, I wouldn't say that static variables marked with
ThreadLocalAttribute are thread safe. As I understand thread safety is that
more than one thread can manipulate the variable without collision. If the
data is thread bound that means it exist only for sepcific thread. In this
case no other thread can use that data. This is no thread safety because
only one thread can use the data.
Well, that would rule Willy's answer incorrect too, of course ;)

In the case of a ThreadStatic variable, it's a variable which can be
used as if it were a normal variable, but happens to have a different
value for each thread. This can be really, really handy in places...

Whether you deem them to be "thread safe" or not really depends on how
you define "thread safe" though, and that doesn't really have any
universal definition that I'm aware of.
My answer to the question is - No, static variables by themselves are not
thread safe.


Certainly not "vanilla" static variables.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '06 #6

"Macca" <Ma***@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com...
Hi,

Are static variables thread safe? I have a static byte array. This will be
accessed from more than one thread. Is it automatically thread safe by
being
static rather than an instance or do i need to write code to make it
thread
safe?


It depends on what you mean by thread-safe.
It wont crash.
Feb 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
4 posts views Thread by Simon Harvey | last post: by
25 posts views Thread by Sahil Malik [MVP] | last post: by
4 posts views Thread by Mantorok | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.