Hacker News new | past | comments | ask | show | jobs | submit login
Coroutines in C (2000) (greenend.org.uk)
10 points by Paul-Craft on July 8, 2023 | hide | past | favorite | 2 comments



I really like the idea of coroutines. I've also implemented an unrolled switch based state machine coroutines in Java that imitates async/await.

I am unsatisfied with coroutines in C, I ported some assembly from a blog post (down atm) to run coroutines in GNU Assembler syntax

https://github.com/samsquire/assembly/blob/main/coroutines.S https://blog.dziban.net/coroutines/ ( down)

I've been thinking of what would be a good syntax for coroutines. I like the design of Erlang, Go and Pony. But I would like a syntax that provides very good control over coroutine hierarchies. Here's some syntax I'm playing with:

I like the idea of Go's channels, for yielding, what if you want to yield to different places? So I'm trying to incorporate that idea.

This example creates 10 producers that talk to 1 consumer:

  producers = []

  task Producer():
    while running:
       output.yield(1)

  task Consumer():
    while running:
       value = input.read()
       print(value)

  for index in range(0, 10):
     producers = Producer()
     producers.append(producer)
     producer.output -> consumer.input

  consumer = Consumer()
This example takes 1 producer and wires them to 10 consumers:

  multiple_consumers = []
  one_producer = Producer()
  for item in range(0, 10):
     consumer = Consumer()
     multiple_consumers.append(consumer)
     one_producer.output -> multiple_consumers
I've been thinking of using the -> operator to wire up channels of coroutines. You can even wire up to a collection of coroutines.

I want to handle 1:1, 1:* and *:1 and *:* and load balancing. How do you suggest I create these hierarchies syntactically?


Two past submissions with comments:

* 07 Feb 2019: https://news.ycombinator.com/item?id=19106796

* 24 May 2020: https://news.ycombinator.com/item?id=23293835




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: