TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Grand Central Dispatch: The Sleeping Barber Problem

46 pointsby koenbokover 15 years ago

2 comments

vilyaover 15 years 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.<p>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.<p>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.<p>I guess it just goes to show that even with Grand Central you still have to be a little careful.
评论 #849287 未加载
vinutherajover 15 years ago
Here's a good discussion on a related topic in the 9fans archive - <a href="http://www.mail-archive.com/9fans@9fans.net/msg10581.html" rel="nofollow">http://www.mail-archive.com/9fans@9fans.net/msg10581.html</a>
评论 #849154 未加载
评论 #849270 未加载