473,549 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2463
No variable is automatically thread-safe, not even static.

"Macca" <Ma***@discussi ons.microsoft.c om> wrote in message
news:65******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:65******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> 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 ThreadLocalAttr ibute 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.co m>
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 ThreadLocalAttr ibute 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 ThreadLocalAttr ibute 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
ThreadLocalAttr ibute 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 ThreadLocalAttr ibute 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 ThreadLocalAttr ibute is. There is no information on such
an attribute in the docs (or at least I can't find it).


Oops, sorry - ThreadStaticAtt ribute.
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
ThreadLocalAttr ibute 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.co m>
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***@discussi ons.microsoft.c om> wrote in message
news:65******** *************** ***********@mic rosoft.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
6883
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 safe public class Cach public static int GetAValue( int x = 0 x = GetRandomValue()
4
1623
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 method is static, do I need to worry about concurrency? I figured that microsoft would have had this in mind when making the component. Obviously...
6
8294
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 fStream=null; lock(fStream) //just in case we use it for multithreading to be thread safe {
9
4148
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 verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I...
25
5127
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
4
2475
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" reference the same static members. For example if I update the static member on one session this change will be reflected on somebody elses session,...
3
2765
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? Testing has shown about the same behavior, am I missiing something?? Thanks --
11
2230
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 multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe...
15
2749
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 Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared...
0
7521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.