-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal for ImmutableInterlocked.ApplyChange API #13949
Comments
Minor naming suggestions/questions:
Should there be an overload that takes a |
Thanks for the suggestions, @justinvp. I've applied those changes. I was thinking about the |
Love this idea. Would be very handy and useful! Btw, could you shed some light on choosing boolean as the return value instead of the updated immutable instance itself? Take the above example:
Do we lose the ability to grab the updated instance right after the operation? Thanks, |
Is the implementation specific to immutable collections at all? Looking at the signature it seems it's not. In that case, the method should probably not live in |
FYI This method can already be found in the VS SDK as ThreadTools.ApplyChangeOptimistically. @karldodd The method returns @terrajobst No, there's nothing actually specific to immutable collections in this method. Is adding it to |
It is; it just might be too late for adding to the .NET Framework 4.6 as we're looking it down very aggressively. However, there is nothing stopping us from adding to .NET Core and later on port to .NET Framework 4.6. |
Where is the source code for |
@AArnott Agree with you. As you pointed out, there is 'a point for returning T'. For the immutable list referenced in this.field:
If we want to grab the new list count caused by this add operation, returning T helps. If returning bool, there is probably no way to tell which operation caused a count increment from x to x+1. So yes, personally I would vote for 'return T and then add another method that wraps this one and returns bool'. Returning T also looks consistent with most methods in static class Interlocked. |
We've reviewed this proposal and think it's a good addition and thus accept PRs. |
Add download page for runtime-only packages
Folks using immutable collections in multi-threaded environments frequently want to leverage the immutability to use lock-free patterns of updating the fields that store those collections.
Interlocked.CompareExchange leaves quite a bit of insight into the runtime and threading requirements to the programmer when it comes to lock-free mutations of immutable collections. While we already have the
ImmutableInterlocked
class with several methods on it, all methods are special purpose and aren't suitable for such simple and common operations as adding or removing an element to anImmutableList<T>
orImmutableHashSet<T>
.I propose adding this API:
The
ImmutableInterlocked.ApplyChange
method makes such lock-free mutations trivial:More complex functions are of course also supported, allowing search of a collection, computation, and a subsequent prescribed change returned. In each case, an "optimistic locking" style transaction is used to execute the provided function (repeatedly, if necessary in the case of a lost race) to update the field without locks.
I plan to send a pull request once this issue is reviewed. I'm attempting to follow the process proposed in #13938 since this change adds to the public API.
Change log:
hotLocation
tolocation
andtransformation
totransformer
.TArg
The text was updated successfully, but these errors were encountered: