Live data from Hacker News

Show HN: Libconcurrent – Coroutines in C

github.com

21–26 of 26 posts

Re: Show HN: Libconcurrent – Coroutines in C

#21
post #8

How does this compare to Apple's GCD [1]? 1 - https://en.wikipedia.org/wiki/Grand_Central_Dispatch

I ported GCD to Linux when it was made available/open-sourced some many months ago. Effectively, GCD uses a bunch of (named) FIFO queues, protected by semaphores, and manages some OS threads, which dequeue tasks(or blocks) from the queues and execute them. It provides some nice higher level abstractions and functionality (e.g groups ) on top of that. It also supports both synchronous and asynchronous execution semantics. The OS threads are of course managed by the OS scheduler. (unfortunately, pthread workqueues are not supported on Linux yet -- the original implemntation creates workqueues for each defined priority and that's how priorities are supported in GCD. On Linux you can use setpriority(), ioprio_set() and other such APIs that don't really offer much in practice. But I digress).

Contrast that to coroutines/fibers where tasks(i.e functions), which run in the same I/O thread(though not an absolute requirement). Cooperative multitasking is used, where a running function can yield control to other runnable functions by explicitly calling a yield like function -- that is, there is no scheduler that preemptively stops a function and runs another. You need to do that yourself.

There is some overlap between the benefits and properties of tasks that run on OS threads and fibers, but for the most part, they have different pros and cons. You can't really use in practice fibers to get the benefits you get from a GCD like framework (which utilizes multiple H/W cores to run functions/tasks concurrently), but also, you can't use GCD to improve throughput on a per-thread basis and deal with long-running and/or blocking lightweight tasks, or implement a fair-scheduling execution scheme).

Re: Show HN: Libconcurrent – Coroutines in C

#22

I wrote a networking daemon (SMTP greylisting proxy; now unmaintained, but http://spey.sf.net ) using coroutines once. Non-preemptive multitasking made reasoning about the problem way easier, because I had no concurrency to worry about, while still allowing inversion of control flow which meant that the SMTP state machines were small and simple. It worked really well. ...until I started getting bug reports from users…

This is the best comment I've read today, thanks.

Re: Show HN: Libconcurrent – Coroutines in C

#23

I wrote a networking daemon (SMTP greylisting proxy; now unmaintained, but http://spey.sf.net ) using coroutines once. Non-preemptive multitasking made reasoning about the problem way easier, because I had no concurrency to worry about, while still allowing inversion of control flow which meant that the SMTP state machines were small and simple. It worked really well. ...until I started getting bug reports from users…

I use pthreads and coroutines (actually fibers) inside pthreads and lock-free bounded queues to communicate between pthreads. All pthreads execute coroutines until all of them get blocked, then pthreads go to epoll. If a pthread gets a socket readiness notification for a socket it owns or an orphan socket then it handles I/O itself, without any synchronization. If a socket is owned by a coroutine running on a different pthread then the notification is forwarded to that pthread via the bounded queue. The project is called MainMemory, and it is just here on Show HN.

Re: Show HN: Libconcurrent – Coroutines in C

#24

How does this compare to http://libmill.org ?

libmill offers a channels(FIFO queues) abstraction. This is a fibers/coroutines framework. They solve different problems. See go-lang channels for what libmill is providing.

Oops, sry, I meant https://github.com/Zewo/libvenice

Re: Show HN: Libconcurrent – Coroutines in C

#25

How does this compare to http://libmill.org ?

libmill offers a channels(FIFO queues) abstraction. This is a fibers/coroutines framework. They solve different problems. See go-lang channels for what libmill is providing.

libmill provides fibers/coroutines as well. check "go" keyword.
Post reply on HN