Home > Blockchain >  Is this some sort of race condition I created with Swift
Is this some sort of race condition I created with Swift

Time:09-12

I have a simple SwiftUI example that moves a block down the screen X pixels each time a timer fires; I wrote the code out in full.

issued here can be considered to be zero; vertiSide horizSide are just reference numbers.

Now this works, but I want to execute it in a loop.

fileprivate func extractedFunc() {
    let t3 = "\(tileP[0   issued].vertiSide - 32)\(tileP[0   issued].horizSide)"
    if let ok = tileD[t3] {
        if !ok {
            var t0 = "\(tileP[0   issued].vertiSide)\(tileP[0   issued].horizSide)"
            tileD[t0] = false
            tileP[0   issued].vertiSide -= 32
            var t1 = "\(tileP[0   issued].vertiSide)\(tileP[0   issued].horizSide)"
            tileD[t1] = true
            
            t0 = "\(tileP[1   issued].vertiSide)\(tileP[1   issued].horizSide)"
            tileD[t0] = false
            tileP[1   issued].vertiSide -= 32
            t1 = "\(tileP[1   issued].vertiSide)\(tileP[1   issued].horizSide)"
            tileD[t1] = true
            
            t0 = "\(tileP[2   issued].vertiSide)\(tileP[2   issued].horizSide)"
            tileD[t0] = false
            tileP[2   issued].vertiSide -= 32
            t1 = "\(tileP[2   issued].vertiSide)\(tileP[2   issued].horizSide)"
            tileD[t1] = true
            
            t0 = "\(tileP[3   issued].vertiSide)\(tileP[3   issued].horizSide)"
            tileD[t0] = false
            tileP[3   issued].vertiSide -= 32
            t1 = "\(tileP[3   issued].vertiSide)\(tileP[3   issued].horizSide)"
            tileD[t1] = true
        }
    }
}

So I wrote this code, which does the same thing [I thought]

            let semaphore = DispatchSemaphore(value: 1)
            DispatchQueue.global().async {
            let t3 = "\(tileP[0].vertiSide - 32)\(tileP[0].horizSide)"
                for i in 0..<tileP.count {
                    semaphore.wait()
                        
                        if let ok = tileD[t3] {
                            if !ok  {
                                let t0 = "\(tileP[i].vertiSide)\(tileP[i].horizSide)"
                                tileD[t0] = false
                                tileP[i].vertiSide -= 32
                                let t1 = "\(tileP[i].vertiSide)\(tileP[i].horizSide)"
                                tileD[t1] = true
                            }
                        }
                    semaphore.signal()
                }
            }

But the output is very different, for version 1 I get this for the tileD array.

Optional(false) 512.00.0
Optional(false) 480.00.0
Optional(false) 448.00.0
Optional(false) 416.00.0
Optional(false) 384.00.0
Optional(false) 352.00.0
Optional(false) 320.00.0
Optional(false) 288.00.0
Optional(false) 256.00.0
Optional(false) 224.00.0
Optional(false) 192.00.0
Optional(false) 160.00.0
Optional(false) 128.00.0
Optional(true) 96.00.0
Optional(true) 64.00.0
Optional(true) 32.00.0
Optional(true) 0.00.0

But for version 2 I get this for the output

Optional(true) 512.00.0
Optional(true) 480.00.0
Optional(true) 448.00.0
Optional(false) 416.00.0
Optional(false) 384.00.0
Optional(false) 352.00.0
Optional(false) 320.00.0
Optional(false) 288.00.0
Optional(false) 256.00.0
Optional(false) 224.00.0
Optional(false) 192.00.0
Optional(false) 160.00.0
Optional(false) 128.00.0
Optional(false) 96.00.0
Optional(false) 64.00.0
Optional(false) 32.00.0
Optional(true) 0.00.0

And for the life of me I cannot see the difference? What am I missing??

CodePudding user response:

What's the semaphore for? There is no asynchronous thread stuff in the code.

Just write

fileprivate func extractedFunc() {
    let t3 = "\(tileP[0   issued].vertiSide - 32)\(tileP[0   issued].horizSide)"
    if let ok = tileD[t3] {
        if !ok {
            for i in 0..<4 {
                let t0 = "\(tileP[i   issued].vertiSide)\(tileP[i   issued].horizSide)"
                tileD[t0] = false
                tileP[i   issued].vertiSide -= 32
                let t1 = "\(tileP[i   issued].vertiSide)\(tileP[i   issued].horizSide)"
                tileD[t1] = true
            }
        }
    }
}
  • Related