Escaping closure captures mutating 'self' parameter (SWIFT 5)

956 Views Asked by At

I'm trying to make a thread-safe struct using the @propertyWrapper, but I have such an error from the playground when I'm setting a value. The error goes, only if I change the async to sync, but I just need to have async function

@propertyWrapper
struct SafeThread<Value>{

    private let queue = DispatchQueue(label: "sellQueue",attributes:.concurrent )
    private var value: Value
    
    
    init (wrappedValue: Value){
        self.value = wrappedValue;
    }
    
    
    var wrappedValue :Value {
        get { queue.sync { value } }
        set { queue.async(flags: .barrier) { self.value = newValue } } // there an error 
        
    }
}

And class where I want to use it :

class Safe{
@SafeThread var foo = 0;
    
func threadSafetyExperiment() {
    
    DispatchQueue.global().sync {

        DispatchQueue.concurrentPerform(iterations: 1_000) { _ in
            self.foo += 1
        }
        print(self.foo)
    }
}
}
1

There are 1 best solutions below

1
On

You need to use nonmutating set and for this to have external storage for wrapped value.

Here is possible way. Tested with Xcode 12 / iOS 14.

@propertyWrapper
struct SafeThread<Value>{

    private class Storage {
        var value: Value
        init(initialValue: Value) {
            value = initialValue
        }
    }
    private let queue = DispatchQueue(label: "sellQueue",attributes:.concurrent )
    
    private var storage: Storage
    init (wrappedValue: Value){
        self.storage = Storage(initialValue: wrappedValue);
    }
    
    var wrappedValue :Value {
        get { queue.sync { self.storage.value } }
        nonmutating set { queue.async(flags: .barrier) { self.storage.value = newValue } }
    }
}