.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Volatile variables and CSharp threads

December 22, 2008 By: Dave

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
    • Two Interfaces. Same Method. Two meanings. - September 29th, 2008
    • Making values nullable - October 9th, 2008
    • CSharp's Property Shortcuts - October 23rd, 2008
    • Readonly variables in CSharp? Really?! - October 29th, 2008
    • Dispose with Using - November 10th, 2008
    • Delegates in .NET - December 4th, 2008
    • Using Sealed in CSharp - December 8th, 2008
    • CSharp checked and unchecked - December 11th, 2008
    • Advanced CSharp - unsafe mode - December 15th, 2008
    • Volatile variables and CSharp threads - December 22nd, 2008
    • What is the global keyword in CSharp? - December 29th, 2008
    • CSharp fixed keyword - January 5th, 2009
    • using - There's more there than you are using - February 2nd, 2009
    • Stackalloc in CSharp - February 16th, 2009
    • Removing Warnings from CSharp Compile Cycle - March 10th, 2009
    • && vs & and | vs ||... What's the difference? - March 16th, 2009
    • Advanced CSharp - yield - March 25th, 2009
    • Just say “No!” to C# Regions? Really?! - April 16th, 2009
    • C# “” better than string.Empty? - April 20th, 2009
    • .Net String Pool – Not Just For The Compiler - April 22nd, 2009
    • CSharp ?? Operator - May 18th, 2009
    • Using VB.NET From CSharp - July 1st, 2009

    If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

    Most Commented Post

    • Object Oriented Programming has Failed Us
    • Microsoft’s Biggest Mistake with ASP.NET
    • WordPress w/ Forms Authentication on IIS6
    Bookmark to:

    Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
    Hide Sites

    One Response to “ Volatile variables and CSharp threads ”

    1. # 1 Dew Drop - December 23, 2008 | Alvin Ashcraft's Morning Dew Says:
      December 23rd, 2008 at 5:32 pm

      [...] Volatile Variables and C# Threads (Dave M. Bush) [...]

    ← Friday Books – jQuery Reference Guide
    jQuery – Creating Plug-ins →
    • Search

    • Subscribe

      U COMMENT
      I FOLLOW

      Subscribe in a reader

      OR

      Subscribe via e-mail

      Enter your email address: 

      Delivered by FeedBurner

       

    • Follow Me

      • Twitter
      • FaceBook
      • Digg
      • StumbleUpon
      • Propeller
      • Delicious
      • Plaxo

       

    • Recent Posts

      • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
      • jQuery – Date Picker
      • Using VB.NET From CSharp
      • iTextSharp – Adding Images
      • Hungarian Notation – Use What Works, Spit Out The Bones
      • Pre Order Windows 7
      • jQuery Dialog – With Validation Controls
      • iTextSharp – The easy way
      • Structure of my ASP.NET Web Applications
      • 35% Off Accronis True Image 2009 Home
      • VB.NET Hide Module Name
      • ASP.NET/VB.NET – Video Training
      • Does jQuery Make Us Lazy?
      • PDFs Using iTextSharp
      • Programming SEO – Ping



    • Advertise on this site through Lake Quincy Media
    • DotNetNuke Sponsor

       

      Most Valuable Blogger
    • Sponsor

    • Categories

      • Advanced CSharp
      • Advanced VB.NET
      • ASP.NET MVC
      • Did you know
      • DotNetNuke – Module Development
      • DotNetNuke – Skinning
      • internationalization
      • iTextSharp
      • jQuery
      • none
      • Seach Engine Optimization
      • Silverlight
      • SQL For Programmers
      • Twitter
      • winforms
    • Cloud

      .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
    • Archives

      • July 2009
      • June 2009
      • May 2009
      • April 2009
      • March 2009
      • February 2009
      • January 2009
      • December 2008
      • November 2008
      • October 2008
      • September 2008
      • August 2008
      • July 2008
      • June 2008
      • May 2008
      • April 2008
      • March 2008
      • February 2008
      • January 2008
      • December 2007
      • November 2007
      • October 2007
    • Meta

      • Log in
      • Entries RSS
      • Comments RSS
      • WordPress.org
      • Privacy Policy
    • Calendar

      December 2008
      S M T W T F S
      « Nov   Jan »
       123456
      78910111213
      14151617181920
      21222324252627
      28293031  
    • Blogroll

      • Alvin Ashcraft’s Morning Dew
      • ASP.NET Consulting
      • Life Hacker
      • Remember Anything
      • The Price of Their Toys
      • Uncategorized Thought


    .NET Answers © 2007 - 2008 All Rights Reserved.
    Entries and Comments.