iOS Swift Stuck When Using A Static pthread_rwlock in Release Build but Debug Build Works Fine


I’m using Xcode15 and targeting iOS 17.
The freeze happens on the Manager.setValue(key:value:) call within the init method of the StaticRwlockApp struct. This issue does not occur when I run the app in debug build configuration.


import SwiftUI

public class Manager {
    
    private static var rwlock: pthread_rwlock_t = {
        var lock = pthread_rwlock_t()
        pthread_rwlock_init(&lock, nil)
        return lock
    }()
    
    private static var sharedDic: [String: String] = [:]
    
    static func getValue(key: String) -> String? {
        pthread_rwlock_rdlock(&rwlock)
        defer {
            pthread_rwlock_unlock(&rwlock)
        }
        return sharedDic[key]
    }
    
    static func setValue(key: String, value: String) {
        pthread_rwlock_wrlock(&rwlock)
        sharedDic[key] = value
        pthread_rwlock_unlock(&rwlock)
    }
}

@main
struct StaticRwlockApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
    
    init() {
        // Release stuck, Debug work
        Manager.setValue(key: "123", value: "123")
    }
}

Stuck stack below
[Release stuck stack](https://i.stack.imgur.com/v0REo.jpg)

P.S.If the Manager is a singleton, the rwlock and func isn't static, the code works fine.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img