

Hexagen: True coroutines for Swift - glittershark
https://github.com/aliceatlas/hexagen

======
Sidnicious
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:

[https://gist.github.com/Sidnicious/8e57892b14629c5ec740](https://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](https://github.com/Sidnicious/team))

~~~
eridius
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](https://gist.github.com/kballard/ff3640e08f2238875680)

------
puls
Previously on HN:
[https://news.ycombinator.com/item?id=9132385](https://news.ycombinator.com/item?id=9132385)

