TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

NET Memory model and the volatile keyword (2010)

35 pointsby FrankyHollywoodover 9 years ago

1 comment

Maultascheover 9 years ago
So from I&#x27;m understanding here, the volatile keyword makes sure that the code always has the latest value when it is read by a thread. Otherwise, you might get a cached value.<p>A lock, on the other hand, does the same thing, but goes further in preventing other threads from changing the value while the lock is in place.<p>So the volatile keyword seems to be great for read-only operations on data shared between threads, but won&#x27;t prevent a race condition if the same code that is reading the data is also updating the value. A lock would be necessary for that.<p>Interesting. I knew about volatile in C++, but I wasn&#x27;t aware that C# had a volatile as well.
评论 #10520894 未加载
评论 #10521099 未加载