Our first example demonstrates the use of a dispatch group, which is similar to a wait group. It allows us to wait for multiple asynchronous tasks to finish.
To run the program, compile it and then execute:
The order of workers starting up and finishing is likely to be different for each invocation.
In this Objective-C version, we use Grand Central Dispatch (GCD) which is the native concurrency framework for iOS and macOS. The dispatch_group_t is analogous to the WaitGroup in Go. We use dispatch_group_enter and dispatch_group_leave to increment and decrement the group’s counter, respectively. The dispatch_group_wait function blocks until all tasks in the group are complete, similar to WaitGroup.Wait() in Go.
Note that Objective-C and the iOS/macOS ecosystem don’t have a direct equivalent to goroutines. Instead, we use GCD’s dispatch queues to perform concurrent operations. This approach provides similar functionality but with a different API and underlying implementation.