Home » Advanced CSharp » Volatile variables and CSharp threads

Volatile variables and CSharp threads

seagull The volatile keyword is a convenience keyword for those who need to access member variables of a class or structure in multi-threaded conditions.

Again, since this is an advanced CSharp concept this is probably something that most of you will not need to worry about using, especially in ASP.NET.  However, there have been times when I’ve used multi-threading in an ASP.NET application (for screen scraping performance) so it is not completely out of the realm of possibility for you to need to know something about how to do multi-threaded programming.  If you do, you’ll be glad you learned about the volatile keyword.

The purpose of the volatile keyword is to tell the compiler that the variable you are marking as volatile may be accessed by multiple threads.  There are certain optimizations that the CSharp compiler makes when it compiles our code and unless the variable is marked as volatile, the compiler will make optimizations assuming that the variable will only be accessed by one thread at a time.

Note that the volatile keyword can only be used against the following types of data:

  • Reference types.
  • Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a “pointer to volatile.”
  • Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
  • An enum type with an integral base type.
  • Generic type parameters known to be reference types.
  • IntPtr and UIntPtr.
  • Variables that are objects or structures will need to use other locking mechanisms.

    Only variables that are members of classes or structures can be declared as volatile.

    To declare a variable volatile, just add volatile as one of the variable modifiers.

    class Demo
        private volatile int m_multiThreadVar;

    Other places talking about Volatile

    • Charlie Calvert’s Community Blog : CSharp Language Specification … – re: CSharp Language Specification, Version 3.0 Available for Review. One week to review 500 pages, I’ll try my best… Section 10.5.3. isn’t very clear. It mentions “acquire semantics” and “release semantics for volatile fields; …
    • delegates and threading, how volatile! – net and specifically c-sharp. for so long i have wondered about threading, particularly as it became clear that intel and amd were moving to multi-core systems, and pushing them on consumers whether the latter wanted them or not. …
    • Double Check Locking and Other Premature Optimizations Can Shoot … – Note that we use the volatile keyword for the _singletonInstance static member. Why? Long story made short, this has to do with how different memory models can reorder reads and writes. For the current CLR you can ignore the volatile …


    Other post in Advanced CSharp

    Related Post

    • CSharp ?? OperatorCSharp ?? Operator One of the problems with learning a language early in its life cycle is that by the time version three comes out, you never look at or implement any of the new features. There are two […]
    • Readonly variables in CSharp? Really?!Readonly variables in CSharp? Really?! I'm sure most of you are familiar with the keyword "const," which allows you to declare a variable and give it a value and assures that no other code will change the value. const int v = […]
    • CSharp fixed keywordCSharp fixed keyword Since I've already mentioned my bias against using unsafe mode in this post:Advanced CSharp - unsafe modeI'll skip my normal rant about that.  Just suffice it to say they don't […]
    • Two Interfaces. Same Method. Two meanings.Two Interfaces. Same Method. Two meanings. We've discussed interfaces before, but today I want to dig a little deeper.  I'm going to assume for now that you already know what an interface is and that you know how to implement one […]
    • Dispose, Finalize and SuppressFinalizeDispose, Finalize and SuppressFinalize I got the following question recently. What is the difference between Dispose and SupressFinalize in garbage collection?” The problem with this question is it assumes Dispose and […]

    About Dave Bush

    Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

    One Pingback/Trackback