Skip to content
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

@OTAtomicsThreadSafe wrapper not usable on local var within a method #1

Open
orchetect opened this issue Feb 19, 2022 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@orchetect
Copy link
Owner

orchetect commented Feb 19, 2022

Issue

In its current implementation, @OTAtomicsThreadSafe is designed to work as a wrapper around variables installed in an object (class, struct, enum, etc.).

success

However it will strangely result in a data race when used on a variable declaration within a function. So this wrapper should not be used in this fashion.

The following code will almost always return a value that is less than 100:

fail

Proposal

It is poor form to declare a local variable within a function and then attempt to mutate it concurrently. Better form is to take the value and pass it into a concurrent operation that can run synchronously within the method, then rely on the operation to return the fully mutated value, often by way of a completion handler closure which can be waited on.

It is not clear if there is a way to prevent the property wrapper from being used on variable declarations inside a method, as the compiler will not stop you from doing it.

@orchetect orchetect added the enhancement New feature or request label Feb 19, 2022
@orchetect orchetect changed the title ThreadSafe property wrapper usable on variable declarations within a method Need secondary @ThreadSafe wrapper usable on var within a method Feb 19, 2022
@orchetect orchetect changed the title Need secondary @ThreadSafe wrapper usable on var within a method @OTAtomicsThreadSafe wrapper not usable on local var within a method Feb 19, 2022
@ryanfrancesconi
Copy link

i remember us talking about that at some point when i was wondering if you could use it inside a function. Didn't we decide the safe way was to wrap the wrapper var in a struct?

@orchetect
Copy link
Owner Author

It's mostly a paradigm shift - the concurrent code should do all its mutation externally, finish up, and return the final result synchronously within the method. But nothing should be concurrently mutating a local variable declared within the function.

A property wrapper is already wrapping the value, so adding more layers of wrapping won't really improve the situation. On the whole, there is no safe way to mutate a local var concurrently.

@orchetect
Copy link
Owner Author

orchetect commented Feb 19, 2022

That's where something like the operations library I've worked on come in handy (OTOperations, based on NSOperation). You can wrap concurrent work in a BasicOperation and execute it synchronously within a method if you choose.

And there's a host of other API that do similar things in the standard lib or otherwise. Again, just a paradigm shift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants