
Let's Build dispatch_queue - ingve
https://www.mikeash.com/pyblog/friday-qa-2015-09-04-lets-build-dispatch_queue.html
======
chrisdevereux
One of the occasionally annoying things about developer communities is their
tendency to present certain technologies as unqualified 'magic' and one should
be thankful for developing for platform x because only it has technology y.
GCD is sometimes presented this way, when it's mostly just a thread pool with
a nice API.

Which is why articles like this are so great. When I was first dipping my toes
into development, Mike Ash's articles were a great help in cutting through the
marketing hype and realising that these are mostly things you could have
implemented yourself given the time and inclination.

~~~
tantalic
While I quite agree with your general point I think GCD is often under
appreciated. Characterizing GCD as "mostly just a thread pool with a nice API"
glosses over the key feature which Mike Ash specifically points out is not in
this implementation: "the number of threads in the global pool scales up and
down with the amount of work to be done and the CPU utilization of the
system." Making the pool aware of the utilization of the whole system as a
whole is the key feature of GCD. This also cuts to why something like this is
best if it is created and promoted by the system vendor: the more GCD is
utilized by the system software and third party applications the better the
system will perform as a whole.

------
twoodfin

                if([_blocks count] < idleThreads && _threadCount < _threadCountLimit) {
    

Shouldn't that be > idleThreads?

~~~
mikeash
Yes indeed! Oddly in the code on GitHub it's correct. I'm not sure how the
mismatch happened. I corrected it in the article as well. Thanks for pointing
that out.

~~~
amelius
Perhaps a problem with substituting the correct HTML entities :)

~~~
mikeash
I used to have a lot of problems with that, due to my crappy home-grown blog
software, but it's pretty solid now. I think what may have happened is I had
it backwards in the code originally, noticed the problem when pasting the code
into the article, then fixed the code but forgot to fix the article. Maybe. It
was late and beer was involved.

------
ridiculous_fish
Nice! dispatch is a lovely API. Concurrent queues are nice to have, but the
real magic is in serial queues.

For example, we can wrap a hash table in a dispatch queue: get() uses
dispatch_sync, set() uses dispatch_async, and boom: our hash table rehashes in
the background, instead of forcing set() to wait.

What are some other APIs that enable you to build a hash table like that?

------
masklinn
For others not knowing the details of Cocoa API and wondering about it,
[NSCondition wait] unlocks the condition's locks and blocks the thread until
the condition variable is signaled, at which point is reacquires the lock and
resumes.

