title: Can volatile keyword be used for building thread safe auto clear cache?
body: <p>I want to code an auto clear cache which in real scenario clears all items in cache once in many hours but has constant read hits. Since clearing and reading are from different threads this data structure needs to be thread safe. Want to avoid locks if possible with this constant read but very seldom write. With _cache marked as volatile and CleartheCache() being only an assignment (atomic) is this <strong>threadsafe</strong> ?</p>
  
  <pre><code>public class AutoClearCache
  {
     volatile IDictionary&lt;Key,Resource&gt; _cache = new Dictionary&lt;Key, Resource&gt;();
  
     public AutoClearCache(TimeSpan autocleanInterval)
     {
         var observable = Observable.Interval(autocleanInterval);
         observable.Subscribe((r) =&gt; CleartheCache());
     }
  
     private void CleartheCache()
     {
         _cache = new Dictionary&lt;Key, Resource&gt;();
     }
  </code></pre>