开发者

What does Volatile Keyword do for non C/C++ background?

Can someone explain what the Volatile keyword use u开发者_开发知识库sed for...for someone coming from a none C/C++ background?

Thanks


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.


It tells the compiler that the field can be changed from different threads, so that it should not do certain optimisations that would keep changes from showing up.

If you for example read a variable inside a loop, the compiler could create code that reads the variable once outside the loop and reuses the value. If the variable is marked as volatile, the compiler won't make that optimisation and the code will read the variable every time that it's used.


It tells the compiler not to make the assumption that the value won't be changed outside the scope of the variable's declaration. This prevents the compiler from making certain optimizations based on this assumption. This is often the case when the variable is pointing to an address in memory that is being updated by the hardware itself.


In C#, the volatile keyword causes reads and writes to a field to be emitted with the "volatile" MSIL prefix.

A volatile read will cause the CPU to completely execute the read instruction before any other subsequent instructions (on any core) that reference memory start to execute.

A volatile write will cause the CPU to wait until all preceding instructions (on all cores) that reference memory have finished executing.

This effectively causes any set of atomic access to a volatile memory location to behave as if the accesses were synchronized. This works without requiring spin locks, or the overhead of kernel supported critical sections (basically it's built into the CPU / memory manager hardware).


It is used in multithreading environment, you can think of it as a mark to the variable, so the value of that variable that marked with volatile will always be reading or writing from the main memory instead of the cache.

The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.MSDN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜