swiftqosdispatchsemaphore

Does QOS based DispatchSemaphore blocks the entire thread pool


let semaphore = DispatchSemaphore(value: 1) 
DispatchQueue.global(qos: .background)

Does this mean if i use this semaphore to make a specific block of resource to be accessed by only one thread at a time, it will block the entire threads running in the background ( or ) only blocks the threads that demands the resources


Solution

  • It will block only the threads that demand already allocated resources. Whenever a thread calls,

    @discardableResult func signal() -> Int 
    

    semaphore count is incremented by 1 and that thread is given access to the critical section. If an upcoming thread calls the same method it will be blocked until the first thread calls

    func wait()
    

    method.