Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
DispatchGroup notify block being called early

Having an issue with DispatchGroup's notify block being called early in my app and made this playground example to experiment. Based on the output, at times it's being called before even the first .leave(). Feels like I'm missing something obvious and now I've looked at it too long.

let s = DispatchSemaphore(value: 1)
let dg = DispatchGroup()
func go() -> Void {
    for i in 1...2 {
        doWork(attemptNo: i, who: "Lily", secs: Double.random(in: 1.0...5.0))
        doWork(attemptNo: i, who: "Emmie", secs: Double.random(in: 1.0...10.0))
        doWork(attemptNo: i, who: "Wiley", secs: Double.random(in: 1.0...3.0))
    }
}
func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    DispatchQueue.global().async {
        dg.enter()
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")

        DispatchQueue.global().asyncAfter(deadline: .now() + TimeInterval(workSecs)) {
            print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
            s.signal()
            dg.leave()
       }
   }
}
go()
dg.notify(queue: .global(), execute: {print("Everyone is done.")})

Sample output:

Emmie wants to work, will wait 4.674405654828654 seconds, attempt #1
Lily wants to work, will wait 1.5898288206500877 seconds, attempt #1
Wiley wants to work, will wait 1.2182416407288 seconds, attempt #1
Lily wants to work, will wait 3.3225083978280647 seconds, attempt #2
Everyone is done.
Wiley wants to work, will wait 2.801577828588925 seconds, attempt #2
Emmie wants to work, will wait 8.9696422949966 seconds, attempt #2
Lily went to work for 3 seconds on task #2
Wiley was denied. No soup for me! Task #1 not going to happen.
Lily was denied. No soup for me! Task #1 not going to happen.
Wiley was denied. No soup for me! Task #2 not going to happen.
Lily is sliding down the dinosaur tail. Task #2 all done!
Emmie went to work for 3 seconds on task #1
Emmie is sliding down the dinosaur tail. Task #1 all done!
Emmie went to work for 2 seconds on task #2
Emmie is sliding down the dinosaur tail. Task #2 all done!

In this case, "everyone is done" happens almost immediately and since the .leave are along with either not getting the semaphore or after the "work" is done, this doesn't make sense. Help please.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your primary issue is that you call dg.enter() in the wrong place. You always want to call enter() before the async call and you want to call leave() when the async call is finished.

As your code is written now, the for loop finishes before the first call to enter is ever made which is why notify is triggered immediately.

func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    dg.enter()
    DispatchQueue.global().async {
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
        sleep(workSecs)
        print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
        s.signal()
        dg.leave()
    }
}

Your code also makes weird use of a semaphore and sleep. I guess this an attempt to simulate a long running background process.

over 4 years ago · Santiago Trujillo Report

0

That is simple, doWork returns before entering the group ...

this should work, as expected

import Foundation

let s = DispatchSemaphore(value: 1)
let dg = DispatchGroup()
func go() -> Void {
    for i in 1...2 {
        doWork(attemptNo: i, who: "Lily", secs: Double.random(in: 1.0...5.0))
        doWork(attemptNo: i, who: "Emmie", secs: Double.random(in: 1.0...10.0))
        doWork(attemptNo: i, who: "Wiley", secs: Double.random(in: 1.0...3.0))
    }
}
func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
    dg.enter()
    DispatchQueue.global().async {
        //dg.enter()
        print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
        if s.wait(timeout: .now() + secs) == .timedOut {
            print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
            dg.leave()
            return
        }
        let workSecs = UInt32(Int.random(in: 1...3))
        print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
        sleep(workSecs)
        print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
        s.signal()
        dg.leave()
    }
}
go()
dg.notify(queue: .global(), execute: {print("Everyone is done.")})

The best way to avoid such a mistake is to use proper API

    func doWork(attemptNo: Int, who: String, secs: TimeInterval) -> Void {
        DispatchQueue.global().async(group: dg) {
            print("\(who) wants to work, will wait \(secs) seconds, attempt #\(attemptNo)")
            if s.wait(timeout: .now() + secs) == .timedOut {
                print("\(who) was denied. No soup for me! Task #\(attemptNo) not going to happen.")
                return
            }
            let workSecs = UInt32(Int.random(in: 1...3))
            print("\(who) went to work for \(workSecs) seconds on task #\(attemptNo)")
            sleep(workSecs)
            print("\(who) is sliding down the dinosaur tail. Task #\(attemptNo) all done!")
            s.signal()
        }
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!