Live data from Hacker News

Show HN: Libconcurrent – Coroutines in C

github.com

11–20 of 26 posts

Re: Show HN: Libconcurrent – Coroutines in C

#11
post #5
post #4

I got a silly question: Is this statement basically a do while/while(true) infinite loop? for (;;) { ... }

Yes. You're likely to run into this construct often in C. Lots of people do it because stupid C compilers produce better code for `for(;;)` than they do for `while(1)` and stupid C compilers used to be very common. It's also one less character spent: A rare win/win.

Also it looks like a spider. That's why I use it, anyway.

Re: Show HN: Libconcurrent – Coroutines in C

#12
post #8

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

GCD is purely callback-based. It works neatly with Apple's "blocks" (inline capturing functions) extension to C, which make dealing with callbacks slightly nicer, but it's still just a sequence of callbacks.

Fully fledged coroutines let you save and resume execution in place. The main advantage is readability of conditional/looping control flow, which tends to quickly devolve into a mess in callback style code.

Re: Show HN: Libconcurrent – Coroutines in C

#13
post #7
post #5

Earlier quoted context omitted.

Yes. You're likely to run into this construct often in C. Lots of people do it because stupid C compilers produce better code for `for(;;)` than they do for `while(1)` and stupid C compilers used to be very common. It's also one less character spent: A rare win/win.

Cool, thanks! I have yet to learn C other than from what I've learned from programming in C++. I am really interested in learning some C however, especially after this post hit front page a few days ago: https://matt.sh/howto-c

  for (;;) { ... }
is a valid JavaScript and PHP syntax too. And used also as the first few chars of a JSON stream to prevent other sites consuming your internal API (e.g. Facebook uses it, Google+ uses while(1) { ... } which is longer).

Re: Show HN: Libconcurrent – Coroutines in C

#16
You might also want to look at lthread. It has some interesting functions like lthread_compute_begin()/end() to run expensive computations inside a coroutine. the compute methods move the coroutine into an actual pthread and resume until compute_end() is called. It also has c++11 bindings called lthread-cpp which allows you to launch coroutines with variable arguments & types and comes with nice socket wrappers.

  void MyMethod(std::vector my_vec) {}
  std::vector v{1,2,3,4};
  Lthread t1{&MyMethod, v};
  t1.detach()

  void my_coroutine()
  {
     lthread_compute_begin();
       ret = fibonacci(55);
     lthread_compute_end();
  }

Re: Show HN: Libconcurrent – Coroutines in C

#20
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 about weird irreproducable crashes. After a very, very long time I eventually figured out that if spey was built on a system where sqlite had been compiled with pthread support, then it automatically linked in a thread-safe malloc, which tried to fetch the current thread ID, which on some versions of pthreads on some Linux architectures on some glibc versions with some kernel versions, was stored at the top of the stack. It used alignment tricks to find this.

So, every time something called malloc(), the libc was doing the equivalent of:

    threadid = *(int*) (alignup(sp, 16MB) - sizeof(int))
Except my stacks weren't allocated through pthreads, so threadid ended up being garbage. Hilarity ensued.

I eventually rebuild the coroutine library to be a wrapper around pthreads with a big lock to ensure that only one would run at a time. Which was kind of evil, but much more reliable. (Previously I was using my own coroutine implementation based on getcontext/setcontext.)

So, uh. I can't remember if I had a point any more; but the pain remains.

Post reply on HN