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. Is assignment to int atomic

Is assignment to int atomic

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studioquestion
8 Posts 6 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.
  • D Offline
    D Offline
    Dudi Avramov
    wrote on last edited by
    #1

    Hi, i work in c++ using Visual Studio 2005. I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic. If yes, can you show me proof for that? It is the same in x64 OS?

    A L 2 Replies Last reply
    0
    • D Dudi Avramov

      Hi, i work in c++ using Visual Studio 2005. I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic. If yes, can you show me proof for that? It is the same in x64 OS?

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      it can be, but i doesn't have to. if you want to be sure use the volatile keyword. [code] volatile int test; int main(int argc, const char *argv[]) { test = 5; // this is atomic! } [/code]

      Don't try it, just do it! ;-)

      C J 2 Replies Last reply
      0
      • A Alexander M

        it can be, but i doesn't have to. if you want to be sure use the volatile keyword. [code] volatile int test; int main(int argc, const char *argv[]) { test = 5; // this is atomic! } [/code]

        Don't try it, just do it! ;-)

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Why the volatile keyword make it atomic?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        J 1 Reply Last reply
        0
        • D Dudi Avramov

          Hi, i work in c++ using Visual Studio 2005. I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic. If yes, can you show me proof for that? It is the same in x64 OS?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Dudi Avramov wrote:

          I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic.

          No, absolutely not. There is much more to atomic operations. 1.)The LOCK[^] prefix must be present to instruct the processor that it has exclusive memory access to the variable location. 2.) Modern processors perform speculative fetching and instruction reordering so the code being executed should be serialized before the atomic operaration with SFENCE[^]/LFENCE[^]/MFENCE[^] The volatile keyword[^] should give the compiler a hint that it needs to prevent out-of-order execution which may result in a Memory Barrier[^]. Interlocked Variable Access[^] Synchronization and Multiprocessor Issues[^] Best Wishes, -David Delaune

          D 1 Reply Last reply
          0
          • A Alexander M

            it can be, but i doesn't have to. if you want to be sure use the volatile keyword. [code] volatile int test; int main(int argc, const char *argv[]) { test = 5; // this is atomic! } [/code]

            Don't try it, just do it! ;-)

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            Not true. All volatile does is to tell the compiler to write the value back to memory when it's changed AND that something else may change the variable. Without volatile, under some circumstance it could optimize it a register and only write and/or fetch it periodically.

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            1 Reply Last reply
            0
            • C CPallini

              Why the volatile keyword make it atomic?

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              Definitely not.

              Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

              1 Reply Last reply
              0
              • L Lost User

                Dudi Avramov wrote:

                I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic.

                No, absolutely not. There is much more to atomic operations. 1.)The LOCK[^] prefix must be present to instruct the processor that it has exclusive memory access to the variable location. 2.) Modern processors perform speculative fetching and instruction reordering so the code being executed should be serialized before the atomic operaration with SFENCE[^]/LFENCE[^]/MFENCE[^] The volatile keyword[^] should give the compiler a hint that it needs to prevent out-of-order execution which may result in a Memory Barrier[^]. Interlocked Variable Access[^] Synchronization and Multiprocessor Issues[^] Best Wishes, -David Delaune

                D Offline
                D Offline
                Dudi Avramov
                wrote on last edited by
                #7

                Is it possible that the mov assembly instruction is not atomic?

                S 1 Reply Last reply
                0
                • D Dudi Avramov

                  Is it possible that the mov assembly instruction is not atomic?

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  It is possible that it's not atomic - if the processor doesn't have the bus locked while performing the MOV, some other bus attached hardware (like, for example, the other core of the processor, or some hardware performing a DMA) could theoretically write to that memory location if it has precedence over the current processor.

                  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