@OTAtomicsThreadSafe
wrapper not usable on local var within a method
#1
Labels
enhancement
New feature or request
Issue
In its current implementation,
@OTAtomicsThreadSafe
is designed to work as a wrapper around variables installed in an object (class, struct, enum, etc.).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:
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.
The text was updated successfully, but these errors were encountered: