locked
Does VolatilveRead/Write method force CPU read/write data from memory? RRS feed

  • Question

  • I was reading CLR via C#, and the book said, using volatile keyword, the JIT will compile the read/write operation to the fields to VolatileRead/Write, and:

     "Furthermore, the volatile keyword tells the C# and JIT compilers not to cache the field in a CPU register, ensuring that all reads to and from the field actually cause the value to be read from memory."

    So, my question is, do VolatilveRead/Write merely acquire/release semantics, and won't force CPU read/write data from memory? If so, then VolatileRead/Write seems still can't resolve the Cache Coherency Problem.

    Sunday, December 4, 2011 7:47 PM

Answers

  • I've find the answer in CLR Via C# 3rd edition (p. 800), which confirmed both VolatileRead and VolatileWrite will cause memory access operation, plus, VolatileRead has the "load acquire" effect, and VolatileWrite has the "store release" effect.

    Thank you guys for your replies, I learnt a lot from your posts. :)

    • Marked as answer by Michael_Peng Thursday, December 8, 2011 11:24 AM
    Thursday, December 8, 2011 11:24 AM

All replies

  • Volitale is used to ensure that a thread doesn't get a cached value where a thread(s) doesn't have to perform a lock on the item in question and writes / read occur immediately. 

    To my knowledge there is no cache coherency due to a compiler optimizing the target item to a non register location; hence it cannot happen unless we are talking about parallel processing (see False Sharing) niche situation.

    HTH


    William Wegerson (www.OmegaCoder.Com)
    Sunday, December 4, 2011 8:09 PM
  • Volitale also ensures that the jitter doesn't reorder the instructions around the statements which do ValitileRead/Write. For more reading http://www.bluebytesoftware.com/blog/2010/12/04/SayonaraVolatile.aspx.
    Wednesday, December 7, 2011 6:51 PM
  • If you have concern about cache coherency, you may want to read this blog post.
    Thursday, December 8, 2011 1:43 AM
    Answerer
  • Volitale is used to ensure that a thread doesn't get a cached value where a thread(s) doesn't have to perform a lock on the item in question and writes / read occur immediately. 

    To my knowledge there is no cache coherency due to a compiler optimizing the target item to a non register location; hence it cannot happen unless we are talking about parallel processing (see False Sharing) niche situation.

    HTH


    William Wegerson (www.OmegaCoder.Com)

    Hi Will, thanks for your quick reply. Your reply didn't answer my question but still taught me a lot, really appreciate you. I've read the False Sharing article mentioned in your post, and got a new question, does the False Sharing problem only apply for the volatile varibles? To be more specific, the article said mordern CPU has a mechanism to maintain cache conherency, will CPU maintain all values in cache or only those marked as volatile? (since I don't see the rand variables are explicitly marked as volatile)
    Thursday, December 8, 2011 9:53 AM
  • I've find the answer in CLR Via C# 3rd edition (p. 800), which confirmed both VolatileRead and VolatileWrite will cause memory access operation, plus, VolatileRead has the "load acquire" effect, and VolatileWrite has the "store release" effect.

    Thank you guys for your replies, I learnt a lot from your posts. :)

    • Marked as answer by Michael_Peng Thursday, December 8, 2011 11:24 AM
    Thursday, December 8, 2011 11:24 AM