3 ms·
The code in the linked article has a bug: it doesn't wait for the queued tasks to complete, so the program will exit while people are still waiting for their ha
by vilya 17y ago
The code in the linked article has a bug: it doesn't wait for the queued tasks to complete, so the program will exit while people are still waiting for their haircuts - or worse, while the barber's actually cutting some hair.
You can fix this by using dispatch_group_async instead of dispatch_async, then after the loop (before releasing the queue) call dispatch_group_wait to make sure all the queued blocks have completed.
This plus creating and releasing the group only adds three lines of code; the rest of the author's algorithm is correct as far I can tell.
I guess it just goes to show that even with Grand Central you still have to be a little careful.
- jonathandann 17y agoNo, you're right, except that the while-loop condition is YES, so it never exits! It's worth noting that all threads in Cocoa (and GCD) are detached pthreads, so whatever you do you have to wait on critical operations. The sample code is technically correct, but your argument is also valid. I was going for a demo of something that applies in the larger scope of real-world apps :) GCD, by no stretch of the imagination, negates the requirement of thought!