473,815 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ThreadStatic attribute

CJ
I came across the ThreadStatic attribute today, but I'm a bit confused
by it.

I understand that it makes a variable static to a particular thread,
so each thread will have it's own value for it (unlike a regular
static variable). But then what makes it different to just an
ordinary class level variable? In other words, what's the difference
between var1 and var2 below?

public class Test
{
[ThreadStatic]
public static int var1;

public int var2;
}
Jun 27 '08 #1
2 4514
On May 7, 10:23 am, CJ <cj_junkt...@ma il.comwrote:
I came across the ThreadStatic attribute today, but I'm a bit confused
by it.

I understand that it makes a variable static to a particular thread,
so each thread will have it's own value for it (unlike a regular
static variable). But then what makes it different to just an
ordinary class level variable? In other words, what's the difference
between var1 and var2 below?

public class Test
{
[ThreadStatic]
public static int var1;

public int var2;

}
Ordinary variables are shared between threads. So, if thread X sets
values for var1 and var2, and thread Y reads them, it will see the new
value for var2, but not the new value for var1.

Jon
Jun 27 '08 #2
CJ
On May 7, 12:12 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On May 7, 10:23 am, CJ <cj_junkt...@ma il.comwrote:
I came across the ThreadStatic attribute today, but I'm a bit confused
by it.
I understand that it makes a variable static to a particular thread,
so each thread will have it's own value for it (unlike a regular
static variable). But then what makes it different to just an
ordinary class level variable? In other words, what's the difference
between var1 and var2 below?
public class Test
{
[ThreadStatic]
public static int var1;
public int var2;
}

Ordinary variables are shared between threads. So, if thread X sets
values for var1 and var2, and thread Y reads them, it will see the new
value for var2, but not the new value for var1.

Jon
Doh! I realised the answer this morning as I woke up :) Clearly I
wasn't thinking yesterday!
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3288
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct link to the gif file. why? <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
4
2936
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are defined in a different namespace from the element.
1
4483
by: Hemanth | last post by:
Hi, I am using .NET 2.0. Suppose I mark a static variable with the ThreadStatic attribute in a class. From some method in the class, if I pass that variable to an object of another class as an argument in a method, would it be maintained as a reference in the other object ? Subsequently, if the second object spawns a thread which also accesses this variable, would the two threads be working on the same instance of the variable ? Or...
1
2043
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
2
6647
by: alexey_r | last post by:
I need to set the value of a ThreadStatic field before starting the thread. According to MSDN, setting the value normally won't work, because this is done only once. Neither will a static constructor. Checking for null every time the field is accessed is too slow. The only idea I have is to use a second, bool, ThreadStatic field to tell me if the first field has been initialized. Is there a better way?
2
1736
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The sam is true of the AttributeUsageAttribute class and on and on. I don't get it. Gary
0
1978
by: Thomas Wittek | last post by:
Hi! I'm using xsl:attribute-sets to reduce redundancy in my XSLT. An example from a transformation to XHTML (the attribute values are simply copied from input to output): <xsl:attribute-set name="cellhalign"> <xsl:attribute name="align"> <xsl:value-of select="@align" /> </xsl:attribute>
6
3565
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider: class X( object ): y = 'private value' def get_y( self ): return self.y
18
6791
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
0
9611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10428
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10145
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9226
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7687
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.