Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How thread safe are static fields?

How thread safe are static fields?

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    In the following struct:

       public struct myStruct
            {
                public static int CumulativeCountOfInstances;
    
                public myStruct(int SomeUnusedData)
                {
                    CumulativeCountOfInstances++;
                }
            }
    

    If concurrent processes are creating myStruct structs, what prevents collisions as each instance tries to increment CumulativeCountOfInstances?

    M 1 Reply Last reply
    0
    • J JoeRip

      In the following struct:

         public struct myStruct
              {
                  public static int CumulativeCountOfInstances;
      
                  public myStruct(int SomeUnusedData)
                  {
                      CumulativeCountOfInstances++;
                  }
              }
      

      If concurrent processes are creating myStruct structs, what prevents collisions as each instance tries to increment CumulativeCountOfInstances?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Nothing prevents it. Maybe a good place for an interlocked increment :) How are different processes sharing the same variable? Or did you mean concurrent threads? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Nothing prevents it. Maybe a good place for an interlocked increment :) How are different processes sharing the same variable? Or did you mean concurrent threads? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        I definitely mean concurrent threads. Friday afternoons are very hard on my concentration. So I should use Interlocked increments from my constructor, AND I should should use Interlocked exchange any time I want to read this value? Dang, static isn't as static as I thought it was :-)

        M 1 Reply Last reply
        0
        • J JoeRip

          I definitely mean concurrent threads. Friday afternoons are very hard on my concentration. So I should use Interlocked increments from my constructor, AND I should should use Interlocked exchange any time I want to read this value? Dang, static isn't as static as I thought it was :-)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          If you use Interlocked.Increment() / Interlocked.Decrement() then the new value will be returned (an increment and a read are combined in one atomic operation). Reading (for integer types) is atomic (except for 64-bit integers on a 32-bit processor) so there's no Interlocked.Read except for longs. You could use Interlocked.Exchange() if that's what you need to do, but it's not appropriate for just reading the value (what value would you exchange?). To write/modify the variable beyond the atomic operations provided by the Interlocked class, you'll need to use some other type critical section, like lock. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            If you use Interlocked.Increment() / Interlocked.Decrement() then the new value will be returned (an increment and a read are combined in one atomic operation). Reading (for integer types) is atomic (except for 64-bit integers on a 32-bit processor) so there's no Interlocked.Read except for longs. You could use Interlocked.Exchange() if that's what you need to do, but it's not appropriate for just reading the value (what value would you exchange?). To write/modify the variable beyond the atomic operations provided by the Interlocked class, you'll need to use some other type critical section, like lock. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            Thanks again. I wondered why there was no Read for Int32.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups