When to use Semaphore instead of Dispatch Group?

前端 未结 4 2045
无人及你
无人及你 2021-01-30 07:16

I would assume that I am aware of how to work with DispatchGroup, for understanding the issue, I\'ve tried:

class ViewController: UIViewController {
    override         


        
4条回答
  •  梦谈多话
    2021-01-30 08:05

    Semaphores and groups have, in a sense, opposite semantics. Both maintain a count. With a semaphore, a wait is allowed to proceed when the count is non-zero. With a group, a wait is allowed to proceed when the count is zero.

    A semaphore is useful when you want to set a maximum on the number of threads operating on some shared resource at a time. One common use is when the maximum is 1 because the shared resource requires exclusive access.

    A group is useful when you need to know when a bunch of tasks have all been completed.

提交回复
热议问题