And I can't deal with your C code either, especially as it's far from short.

I never claimed that creating a multi-tasking system would be trivial? 60 lines is pretty short, and I presented it in even smaller sections.

Besides, the point is the following producer-consumer example runs and I only used the principles I said I used:

void *main_thread( void* dummy ) { (void)dummy; // Create the threads. ThreadId thread1_id = create_thread( func1, NULL ); ThreadId thread2_id = create_thread( func2, NULL ); // Wait for them to finish. wait_for_thread( thread1_id ); wait_for_thread( thread2_id ); return NULL; } void *func1( void *dummy ) { (void)dummy; for ( int i=5; i--; ) { printf( "func1\n" ); if ( i ) thread_sleep( 1 ); } return NULL; } void *func2( void *dummy ) { (void)dummy; for ( int i=5; i--; ) { printf( "func2\n" ); if ( i ) thread_sleep( 1 ); } return NULL; }

I disagree, most probably your assumptions are based on some C lang specific conventions including call stack manipulations.

The first line from your link: "Coroutines are computer program components that allow execution to be suspended and resumed".

Suspending and resuming a function requires saving where it's at (execution pointer) and it's state (call stack incl lexicals).

Nothing about C or any other language here. You'll find the same thing in Perl coros (Coro) and everywhere else coros exist.

The first yield-to example with routines translates 1to1

Neither examples can be achieved using goto &sub. goto &sub can't be used to jump into the middle of a sub from outside a sub, much less restore the existing lexical state that existed when that point in the sub was previously reached. And I have no idea what you think would save that state in the first place.


In reply to Re^7: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases) by ikegami
in thread Weird syntax. What does this goto statement do? by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.