lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Johnson Lin wrote:
> However, the biggest problem I bumped into right now is there seems to
> have no robust C coroutine implementation. I need asymmetric coroutine
> support and value passing (just like the Lua one), and can be compiled
> across 3 major platforms (windows, osx and linux).

A while back I wrote a coroutine library using
setcontext()/getcontext(), for a project I was doing in C++ (which, it
turns out, would have been much easier in Lua, but that's beside the point).

The lesson I learned from this is: don't use setcontext()/getcontext().
Some Linux libc versions do not allow you to use pthreads functions from
stacks that were not allocated through pthreads. (Because they use magic
numbers at the top of the stack to identify which thread is running.)
This means that if you call a function in any library which has been
compiled with pthreads support, like malloc(), really bad things happen.

I eventually ended up rewriting everything to use multiple pthreads and
a big global lock to force one thread to run at a time. As what I was
using the coroutines for was to implement cooperatively scheduled
threads, this was no big problem, but this technique probably isn't
suitable for other projects.

┌─── ───── ─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup

Attachment: signature.asc
Description: OpenPGP digital signature