A simple version of iOS dispatch_group

Nguyen Tuan Minh
1 min readSep 26, 2021

--

Inspired by Mike Ash’s let’s build series, I write this article as my initial self-build in a simple version.

Firstly, what dispatch_group_t is ? It is easy to read Apple’s code and figure out, it is just an NSObject object , confronting OS_dispatch_group_protocol.

Then what should my dispatch_group (mn_dispatch_group) be? It of course can be like dispatch_group_t, but to make things simple, I created a new class called MNDispatchGroup with three API: enter, wait, and leave.

And the main part is the mechanism for making a barrier and to signal the barrier to continue. I chose C++’s condition_variable(cv), which works with unique_lock . While the unique_lock variable locks the mutex, the cv provides a wait API, which stops the current running thread and the notify API to pass it. This is mn_barrier. The source code is in the attached link.

Finally, it may need some more measurements to know exactly if this MNDispatchGroup really is as fast as dispatch_group_t. Besides there are other tools for further research like C++20’s latch or barrier.

Source code for mn_barrier : https://github.com/mihozil/mn_barrier/tree/main

--

--

Nguyen Tuan Minh

iOS developer | Meditator. Be fond of logical and deep research