How many threads should Grand Central Dispatch be creating?

1.4k Views Asked by At

I understand that GCD will only create as many threads as needed to make best use of the CPU. In code using dispatch_async to launch about 30 background tasks, I'm seeing the number of threads jump by about 30 in Activity Monitor. I would not have expected that, since it's only a dual core PC.

I'm sure I'm misunderstanding something. Can someone tell me what is going on?

1

There are 1 best solutions below

3
On BEST ANSWER

One situation where GCD will increase the thread pool by adding more threads is I/O contention. If a dispatched block waits for filesystem or networking I/O, it doesn’t use the CPU, hence GCD thinks the CPU is idle and able to process more threads.

In fact, depending on the nature of the dispatched blocks, this can increase I/O contention further and reach the limit of 512 worker threads. Mike Ash has written a blog post about this situation.