0

Let's say I have a complex object that is immutable that stores a bunch of data. Let's call this type MyDataCache.

Now, let's say I have a MyDataCacheManager which holds a reference to a MyDataCache. Callers can ask the manager for a reference of the cache and it will give that. There are 1000s of calls per second to get a reference for reading from the cache.

Very infrequently (once every few hours), the cache needs to be updated. So my thinking is, a new MyDataCache is created in the background with the refreshed data and then the reference inside the manager is swapped.

However, I need this to be thread safe. Given, MyDataCache is immutable can I create a method in the manager that simply takes a new MyDataCache as a parameter and sets it as its new internal reference? Given it's just a reference switch of an immutable object, would this be thread safe?

If not, can I use Interlocked.Exchange to achieve this?

Ultimately, given 1000s of reads per second and only 1 update every few hours, what's the best thread safety strategy for this?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
millie
  • 2,642
  • 10
  • 39
  • 58
  • 2
    I would think that `Interlocked.Exhange(ref T, T)` (https://learn.microsoft.com/en-us/dotnet/api/system.threading.interlocked.exchange?view=net-7.0#system-threading-interlocked-exchange-1(-0@-0) ) would seem to fit the bill – Flydog57 Feb 15 '23 at 23:05
  • Side note: This is the real case of micro-optimization - looking into optimizing something that takes an order of 10 ^-6 (aka "micro-") of program's execution time :) ... Presumably you know that reference-size updates are atomic, so before the question can be really answered you need to define what would be "non-thread-safe" behavior for you. Consider [edit] to justify trying to write potentially incorrect code for something that happens so infrequently (1000s times a second) as well as clearly specifying what you consider to be incorrect update. – Alexei Levenkov Feb 16 '23 at 02:38
  • @Flydog57 OP did not specify anything outside vague "thread safe" - and there is no way to update reference-sized variable in "thread unsafe" manner (as these updates are atomic). It is possible that update behavior would not be observed for some time, but generally those are not considered "thread safety" issues, especially for caching code. – Alexei Levenkov Feb 16 '23 at 02:40
  • Does the `MyDataCacheManager` hold the `MyDataCache` reference in a normal field or a [`volatile`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/volatile) field? – Theodor Zoulias Feb 16 '23 at 03:17
  • Possibly related: [Difference between Interlocked.Exchange and Volatile.Write?](https://stackoverflow.com/questions/12425738/difference-between-interlocked-exchange-and-volatile-write) – Theodor Zoulias Feb 16 '23 at 03:21
  • Whatever you do, you should do it in a safe, maintainable and tested way. Don't be clever. – JHBonarius Feb 16 '23 at 07:21

0 Answers0