-
Notifications
You must be signed in to change notification settings - Fork 217
Is GetOrCreate thread-safe? #359
Comments
Do you mean to ask if it guarantees nothing else will modify the same cache key? No, it doesn't. Other threads may attempt to create an entry with the same key at the same time. Only the low level cache operations are thread safe such as Get or Set, and only in that they will not corrupt the cache. When multiple threads are operating on one key there's no guarantee which will win. |
Thanks, I misunderstood how it works. |
even though this issue is closed, I think it's it should be reopened. moreover, AFAIK the .net framework equivalent was thread safe relevant discussion on stack overflow |
@amit-ezra |
@sebastienros Maybe it's best if i give an example.
you would find that you get different results for you run the same code (with minor adjustments) with I think |
I see, you mean the lambda can be called multiple times in both cases, but the same value it returned anyway? Unlike with |
exactly. |
and this would be the same with |
I wonder if GetOrCreate extension method thread-safe?
From the source code it seems to perform 2 steps separately - check if key exists and then add if not. No lock is being used, so is it thread-safe?
The text was updated successfully, but these errors were encountered: