objc_sync_enter / objc_sync_exit not working with DISPATCH_QUEUE_PRIORITY_LOW
<p>I need a read\write lock for my application. I've read  <a href="https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock" rel="noreferrer">https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock</a></p>

<p>and wrote my own class, cause there are no read/write lock in swift </p>

<pre><code>class ReadWriteLock {

    var logging = true
    var b = 0
    let r = "vdsbsdbs" // string1 for locking
    let g = "VSDBVSDBSDBNSDN" // string2 for locking

    func waitAndStartWriting() {
        log("wait Writing")
        objc_sync_enter(g)
        log("enter writing")
    }


    func finishWriting() {
        objc_sync_exit(g)
        log("exit writing")
    }

    // ждет пока все чтение завершится чтобы начать чтение
    // и захватить мютекс
    func waitAndStartReading() {

        log("wait reading")
        objc_sync_enter(r)
        log("enter reading")
        b++
        if b == 1 {
            objc_sync_enter(g)
            log("read lock writing")
        }

        print("b = \(b)")
        objc_sync_exit(r)
    }


    func finishReading() {

        objc_sync_enter(r)

        b--

        if b == 0 {
            objc_sync_exit(g)
            log("read unlock writing")
        }

        print("b = \(b)")
        objc_sync_exit(r)
    }

    private func log(s: String) {
        if logging {
            print(s)
        }
    }
}
</code></pre>

<p>It works good, until i try to use it from GCD threads. </p>

<pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
</code></pre>

<p>When i try to use this class from different async blocks at some moment it allows to write when write is locked</p>

<p>here is sample log:</p>

<pre><code>wait reading
enter reading
read lock writing
b = 1
wait reading
enter reading
b = 2
wait reading
enter reading
b = 3
wait reading
enter reading
b = 4
wait reading
enter reading
b = 5
wait reading
enter reading
b = 6
wait reading
enter reading
b = 7
wait reading
enter reading
b = 8
wait reading
enter reading
b = 9
b = 8
b = 7
b = 6
b = 5
wait Writing
enter writing
exit writing
wait Writing
enter writing 
</code></pre>

<p>So, as you can see g was locked, but objc_sync_enter(g) allows to continue. 
Why could this happen ?</p>

<p>BTW i checked how many times ReadWriteLock constructed, and it's 1.</p>

<p>Why objc_sync_exit not working and allowing to objc_sync_enter(g) when it's not freed ?</p>

<p>PS Readwirtelock defined as </p>

<pre><code>class UserData {

    static let lock = ReadWriteLock()
</code></pre>

<p>Thanks.</p>