synchronizing simple types
-
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
-
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
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
-
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
But I think i need to declare the static variable with the keyword volatile, or is this not correct? Konrad
-
But I think i need to declare the static variable with the keyword volatile, or is this not correct? Konrad
yeah it should be volatile.
Jo hoga, Kuddhaye manjur hoga.
-
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
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
-
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
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
-
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
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."
-
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."
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
-
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
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
-
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
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."