
Daniel James wrote:
Giovanni P. Deretta wrote:
IIRC there was an article in C++ user's journal which implemented coroutines using fibres on windows and threads on unix. I'll have a look for it later. Also there are these article on fibres (these are the 2nd and 3rd parts, the first doesn't seem to be available):
http://blogs.msdn.com/oldnewthing/archive/2004/12/30/344281.aspx http://blogs.msdn.com/oldnewthing/archive/2004/12/31/344799.aspx
Interesting, I'll need to look at the Win32 fiber api more throughly.
Make sure you read the third part - it demonstrates that fibre-safety is very different to thread safety (similar warning apply to coroutines in general).
From a quick look at the article seems that many of these fibre pitfalls are just an artifact of the win32 continuation implementation. Anyway, i think that it is *much* easier to write fibre-safe code than thread-safe code (yay, no locks!).
I think setjump would also have some serious problems - you could only have a single coroutine at a time and it could lead to undefined behaviour in lots of nasty ways.
Well setjump by itself let you only unwind the stack, but used with the sigaltstack trick (see the paper at the Pth site http://www.gnu.org/software/pth/rse-pmt.ps ) you can implement full coroutines. It is portable to all POSIX systems and is probably even more portable than the POSIX makecontext API itself, that not only is slower (it requires a system call to save/restore the sigmask), but it has also been marked as obsolescent in the lastest Single Unix Specification because of ISO C changes). Regards, PS Btw, after reading some articles about the C# yield it seems it is *not* a true form of coroutine but it is limited to iterators (the compiler just rewrites the code inside out). ---- Giovanni P. Deretta