Wouldn't it be possible to implement all of the same semantics in pure Swift using a concurrent dispatch queue and some semaphores? I tried making a generator like the one in Hexagen:
…and I don't know the performance characteristics of Grand Central Dispatch. Could someone who's more familiar with GCD explain why using it for this kind of concurrency is an okay/bad idea and how it compares to coroutines?
Every time you block while on a dispatch queue, you're suspending the entire OS thread. GCD will spin up new threads to service queues when the old ones are blocked, but it won't do this forever. At some point you'll hit a limit where GCD refuses to spin up any new threads and you can't make any progress until one of your existing queues resumes doing work.
You can test this yourself. Experimentally, the limit on OS X 10.10.2 (with the global background queue) is 64 blocked threads. I used the following program to see that: https://gist.github.com/kballard/ff3640e08f2238875680
3 comments
[ 3.0 ms ] story [ 18.6 ms ] threadhttps://gist.github.com/Sidnicious/8e57892b14629c5ec740
I understand the main benefits of coroutines as:
- Cheap creation
- Cheap context switching
- Predictable context switching
…and I don't know the performance characteristics of Grand Central Dispatch. Could someone who's more familiar with GCD explain why using it for this kind of concurrency is an okay/bad idea and how it compares to coroutines?
(FWIW I'm a huge fan of hacking async semantics into languages and made a thing like this for C++: https://github.com/Sidnicious/team)
You can test this yourself. Experimentally, the limit on OS X 10.10.2 (with the global background queue) is 64 blocked threads. I used the following program to see that: https://gist.github.com/kballard/ff3640e08f2238875680