I am wondering about the following scenario.
I have a DispatchGroup
inside a function. Now I am entering the group on a background thread and calling wait()
.
func test() {
let group = DispatchGroup()
DispatchQueue.global().async {
group.enter()
group.wait()
}
}
My question is whether the group will wait forever or get deallocated after the main thread leaves the function?
I am not sure if there is any referencing or capturing. I would be very thankful for some explanation, this topic confuses me a bit.
Thank you in advance!
Yes, it will wait forever.
The group
is captured, so it will not get released. And, obviously, you will end up lock that particular worker thread. As worker threads are quite limited, this is a bigger concern than the amount of memory consumed by the DispatchGroup
.
If you want it to timeout, use wait(timeout:)
.