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. synchronizing simple types

synchronizing simple types

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • K Offline
    K Offline
    Konrad Windszus
    wrote on last edited by
    #1

    Do I need to synchronize simple data types (int, DWORD, bool etc.), when I write to the (static) variable in one thread and read it in another. Does windows ensure that it will not switch to another thread during writing of one value? What do I have to remark. Thanks for any answer. Regards Konrad

    P J 2 Replies Last reply
    0
    • K Konrad Windszus

      Do I need to synchronize simple data types (int, DWORD, bool etc.), when I write to the (static) variable in one thread and read it in another. Does windows ensure that it will not switch to another thread during writing of one value? What do I have to remark. Thanks for any answer. Regards Konrad

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      different thread will not write or read or write/read the values at the same time... but a thread can set a bool value to false just after you have read it as true. So that could cause a problem, Its better to have critical sections for these static variables.


      P.R.A.K.A.S.H

      K 1 Reply Last reply
      0
      • P Prakash Nadar

        different thread will not write or read or write/read the values at the same time... but a thread can set a bool value to false just after you have read it as true. So that could cause a problem, Its better to have critical sections for these static variables.


        P.R.A.K.A.S.H

        K Offline
        K Offline
        Konrad Windszus
        wrote on last edited by
        #3

        But I think i need to declare the static variable with the keyword volatile, or is this not correct? Konrad

        P 1 Reply Last reply
        0
        • K Konrad Windszus

          But I think i need to declare the static variable with the keyword volatile, or is this not correct? Konrad

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          yeah it should be volatile.


          Jo hoga, Kuddhaye manjur hoga.

          1 Reply Last reply
          0
          • K Konrad Windszus

            Do I need to synchronize simple data types (int, DWORD, bool etc.), when I write to the (static) variable in one thread and read it in another. Does windows ensure that it will not switch to another thread during writing of one value? What do I have to remark. Thanks for any answer. Regards Konrad

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

            Look at the InterlockedExchange() and related calls. (on a 80x86 based system, a bool or char will be read/written in an atomic operation, though I don't advise it outside of very narrow circumstances.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            K 1 Reply Last reply
            0
            • J Joe Woodbury

              Look at the InterlockedExchange() and related calls. (on a 80x86 based system, a bool or char will be read/written in an atomic operation, though I don't advise it outside of very narrow circumstances.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

              K Offline
              K Offline
              Konrad Windszus
              wrote on last edited by
              #6

              Thank you very much, that helps. But with InterlockExchange I must write a new value to a variable. What if I just want to read variable and ensure that this is atomic? Do the variable have to be declared as volatile. Sorry but the MSDN library is very short concerning this issue. Konrad

              R 1 Reply Last reply
              0
              • K Konrad Windszus

                Thank you very much, that helps. But with InterlockExchange I must write a new value to a variable. What if I just want to read variable and ensure that this is atomic? Do the variable have to be declared as volatile. Sorry but the MSDN library is very short concerning this issue. Konrad

                R Offline
                R Offline
                Robert A T Kaldy
                wrote on last edited by
                #7

                If you only read the variable, you don't need to synchronize anything :) If you simultaneously read and write to the variable, only the writing operation should be interlocked. Robert-Antonio "Life is very hard, when you apply E-R model to it."

                K 1 Reply Last reply
                0
                • R Robert A T Kaldy

                  If you only read the variable, you don't need to synchronize anything :) If you simultaneously read and write to the variable, only the writing operation should be interlocked. Robert-Antonio "Life is very hard, when you apply E-R model to it."

                  K Offline
                  K Offline
                  Konrad Windszus
                  wrote on last edited by
                  #8

                  Thanks for your answer, but in the MSDN Library I found the following sentence: Simple reads and writes to properly-aligned 32-bit variables are atomic. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interlocked_variable_access.asp What about other variables like char (8bit)? Isn't it possible, that during the read process another thread writes to the variable with InterlockedExchange. After this the read thread is reading the second part of the variable. It gets corrupted, because it wasn't an atomic read. What about other variables like 8, 16 bit. Are reads always atomic? And do I need volatile for these variables if I write to them only via InterlockedExchange. Thanks for any answer Konrad

                  J R 2 Replies Last reply
                  0
                  • K Konrad Windszus

                    Thanks for your answer, but in the MSDN Library I found the following sentence: Simple reads and writes to properly-aligned 32-bit variables are atomic. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interlocked_variable_access.asp What about other variables like char (8bit)? Isn't it possible, that during the read process another thread writes to the variable with InterlockedExchange. After this the read thread is reading the second part of the variable. It gets corrupted, because it wasn't an atomic read. What about other variables like 8, 16 bit. Are reads always atomic? And do I need volatile for these variables if I write to them only via InterlockedExchange. Thanks for any answer Konrad

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

                    You do not need to use volatile if using InterlockedExchange. Reads/writes to/from 8 bit values are atomic on x86 platforms. Do note that if you must guarantee a specific sequence of reading and writing, you'll need to use more complex synchronization methods. 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
                    • K Konrad Windszus

                      Thanks for your answer, but in the MSDN Library I found the following sentence: Simple reads and writes to properly-aligned 32-bit variables are atomic. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interlocked_variable_access.asp What about other variables like char (8bit)? Isn't it possible, that during the read process another thread writes to the variable with InterlockedExchange. After this the read thread is reading the second part of the variable. It gets corrupted, because it wasn't an atomic read. What about other variables like 8, 16 bit. Are reads always atomic? And do I need volatile for these variables if I write to them only via InterlockedExchange. Thanks for any answer Konrad

                      R Offline
                      R Offline
                      Robert A T Kaldy
                      wrote on last edited by
                      #10

                      I think you're right. 8- and 16-bit writing should be atomic too. The operation is made by only one assembler instruction MOV. Volatile declaration is necessary, because it tells to compiler, that its value may be changed every time by another thread. For example, if you're referring to the variable multiple times in one thread, the compiler should optimize it by storing its value in register. If another thread changes the value, you have a different value in memory and register and the program could not behave correctly. Volatile declaration ensures, that the value of variable is always taken from the memory. Robert-Antonio "Czech Railways discovered, that in case of disaster the most damaged wagons were the first and the last. So they decided to create trains without them."

                      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