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 / C++ / MFC
  4. Atomic operations in Win32

Atomic operations in Win32

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 4 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.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    Is reading/writing a 32-bit integer on Win32 guaranteed to be atomic ? I could not find that information anywhere. wbr Brainley

    M S 2 Replies Last reply
    0
    • M Mr Brainley

      Is reading/writing a 32-bit integer on Win32 guaranteed to be atomic ? I could not find that information anywhere. wbr Brainley

      M Offline
      M Offline
      Mr Brainley
      wrote on last edited by
      #2

      Ok, sometimes i'm posting over-eagerly. Here is the solution : Interlocked Variable Access[^] In short : the answer is yes. wbr Brainley

      1 Reply Last reply
      0
      • M Mr Brainley

        Is reading/writing a 32-bit integer on Win32 guaranteed to be atomic ? I could not find that information anywhere. wbr Brainley

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        No. See here[^] for information on how to do it safely. If your programming x86 machine code you could also look up the lock prefix[^].

        Steve

        M 1 Reply Last reply
        0
        • S Stephen Hewitt

          No. See here[^] for information on how to do it safely. If your programming x86 machine code you could also look up the lock prefix[^].

          Steve

          M Offline
          M Offline
          Mr Brainley
          wrote on last edited by
          #4

          I'm writing a procedure where two values, wich always make up a pair, can be changed from one thread, but read from many. I only want to make shure that a read-operation always returns a valid pair (writing is actually limited by a CRITICAL_SECTION). So i use a checksum. The thread reads the variables and then checks the sum against a checksum. If one of the values got changed somewhere in between, the checksum will not match and it will try again. This works, since simple read/write operations on 32-bit Integers are atomic on 32-bit systems. I chose this way because it is much faster that using a lock mechanism, since write operation happen a lot less often (> factor 20) than write-operations.

          M S 2 Replies Last reply
          0
          • M Mr Brainley

            I'm writing a procedure where two values, wich always make up a pair, can be changed from one thread, but read from many. I only want to make shure that a read-operation always returns a valid pair (writing is actually limited by a CRITICAL_SECTION). So i use a checksum. The thread reads the variables and then checks the sum against a checksum. If one of the values got changed somewhere in between, the checksum will not match and it will try again. This works, since simple read/write operations on 32-bit Integers are atomic on 32-bit systems. I chose this way because it is much faster that using a lock mechanism, since write operation happen a lot less often (> factor 20) than write-operations.

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

            How can it get changed if only one thread ever changes it? How can you be sure it doesn't get changed while computing the checksum? Is a critical section really that slow?

            "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              How can it get changed if only one thread ever changes it? How can you be sure it doesn't get changed while computing the checksum? Is a critical section really that slow?

              "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

              M Offline
              M Offline
              Mr Brainley
              wrote on last edited by
              #6

              Here a scenarion. Thread A reads, Thread B writes, the pair consists of V1 and V2 of type int. A reads V1 B writes V1 B writes V2 A reads V2 So now A has a pair that does not belong together. Now consider this : A reads V1 B writes Checksum V1+V2 B writes V1 B writes V2 A reads V2 A checks the Sum -> does not match, so A tries the read again. There are a lot more cases, but they all work out. Using a CRITICAL_SECTION or any other kind of synchronization would mean traps into the OS and context-switches on the wait-operations. In cases where such an optimistic approach is possible, that means the probability of an error, and thus a second or even third read is very low, the performance is higher this way.

              J 1 Reply Last reply
              0
              • M Mr Brainley

                Here a scenarion. Thread A reads, Thread B writes, the pair consists of V1 and V2 of type int. A reads V1 B writes V1 B writes V2 A reads V2 So now A has a pair that does not belong together. Now consider this : A reads V1 B writes Checksum V1+V2 B writes V1 B writes V2 A reads V2 A checks the Sum -> does not match, so A tries the read again. There are a lot more cases, but they all work out. Using a CRITICAL_SECTION or any other kind of synchronization would mean traps into the OS and context-switches on the wait-operations. In cases where such an optimistic approach is possible, that means the probability of an error, and thus a second or even third read is very low, the performance is higher this way.

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                The spin count on a CS object can be utilitized to handle situations like this, but you really need to be on a multi-CPU/multi-core machine to really make use of it.    If the CS is released during the spin count, no context-switch will take place.  Given that simple operations are being done, I would bet that you would hit that situation more often than not.    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                M 1 Reply Last reply
                0
                • J James R Twine

                  The spin count on a CS object can be utilitized to handle situations like this, but you really need to be on a multi-CPU/multi-core machine to really make use of it.    If the CS is released during the spin count, no context-switch will take place.  Given that simple operations are being done, I would bet that you would hit that situation more often than not.    Peace!

                  -=- James
                  Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  See DeleteFXPFiles

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

                  I just hijacked the thread for my own personal learnin'... Can you explain what the heck you're talking about? :) *edit* Google is my friend: Critical Section Objects[^] :rolleyes: Thanks! Mark

                  "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                  1 Reply Last reply
                  0
                  • M Mr Brainley

                    I'm writing a procedure where two values, wich always make up a pair, can be changed from one thread, but read from many. I only want to make shure that a read-operation always returns a valid pair (writing is actually limited by a CRITICAL_SECTION). So i use a checksum. The thread reads the variables and then checks the sum against a checksum. If one of the values got changed somewhere in between, the checksum will not match and it will try again. This works, since simple read/write operations on 32-bit Integers are atomic on 32-bit systems. I chose this way because it is much faster that using a lock mechanism, since write operation happen a lot less often (> factor 20) than write-operations.

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    Looks like you should do some research on "readers writer locks".

                    Steve

                    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