Can someone explain what the Volatile keyword use used for…for someone coming from a none C/C++ background?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re not from a C/C++ background the simple answer is don’t use volatile.
What it does is pretty much implementation dependant at the machine code level. But conceptually it’s telling the complier that the value of the variable might change at any point in time. Perhaps due to the operating system, or due to another thread. So whenever the value needs to be accessed make sure to read the actual memory location and don’t try to be cute by caching it in registers or such like.
Ah! Only just notice the C# tag. In this case it’s more to do with multi-threading and locking (or not locking) than OS issues. Unless you’re doing something with unsafe code.
Re. C# specifically:
http://msdn.microsoft.com/en-us/library/x13ttww7(v=vs.71).aspx
You would hope that when writing C# you can ignore these kind of close to the metal issues and get on with developing well architected applications.