473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2449
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Is a static method that uses local variables thread safe (eg in a web service) In the following code assuming GetRandomValue() and DoSomethingElse() are thread safe, is the static method thread...
4
by: Simon Harvey | last post by:
Hi all, I'm considering using a the micorsoft data access block as the main DAC for my application. A colleage of mine suggested that I might have a problem with concurrency though When a...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
4
by: Mantorok | last post by:
Hi I have an ASP app which references a few static properties in some of the classes. I understand that you should use Session variables, but is it "possible" to have each session "not"...
3
by: inetmug | last post by:
Howdy All" Other then the automatic expiration stuff that you can set when using the data cache, what the the technical advantages of using the data cache versus static application variables? ...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.