1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Co-Routine in C programming

Discussion in 'Microcontrollers' started by Parth86, Jun 2, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Hi
    I am trying to understand what is Co-Routine in Embedded Programming. Co-routine in program allow multiple entry points for suspending and resuming execution at certain locations. I was looking small example to understand co-routine. I found this code example. but I don't understand how does co-routine work in real program I am trying to make small embedded program please help me to implement co-routine in c program related with embedded task.
    Code (c):
     
    #include <setjmp.h>

    int main (void)
    {
      jmp_buf buf1;
     
      if (setjmp(buf1) == 0)
      {
       /* This code is executed on the first call to setjmp. */

       longjmp(buf1, 1);
      } else {
       /* This code is executed once longjmp is called. */
      }
      return 0;
    }
     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Don't go there!!!

    These examples are for higher level programming.... If you start introducing jumps into C a whole heartache of misery will come to light....

    C works with a software stack!! Entering a sub routine / function / procedure certain housekeeping tasks must be done.. This kind of programming dismisses all this... I wouldn't entertain it and I'm pretty well versed in assembler..

    I don't even use goto's in C....

    If your first question is :- "What's a software stack" .. Then I rest my case!
     
    • Like Like x 1
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Thanks for your quick response. actually I was reading topic "Co-routine". than I searched on google. I was looking for specific example that define what is co-routine and when does it use in program and if I understand basic than I tried to implement in program. Software stack are different topics. If you can help with hand made code or algorithm means just want to understand first what is co-routine and when does it use in program?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    Software stacks in C are used for calling functions... Coroutines are functions that have several entry points, you need to tread very carefully as they do not keep the stack updated..

    There are far better ways in C to achieve dynamic programming.. The best in my view are function pointers and pointer pointers...

    I think coroutines are mainly used in RTOS environments where multiple stacks are used.. Jumping from task to task outside of the normal kernel. Even the guy's who implemented this form of access fear its use..

    As far as I'm concerned, Leaving a stack untouched and leaving a function to enter another mid point requires a third stack to oversee the procedure.. Messy.. very messy and I haven't the knowledge to help... The only other guy who messes around with this stuff is MrT and I haven't seen him for a while..
     
    • Like Like x 1
  6. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    337
    Likes:
    36
    Location:
    UK
    I agree with Nigel. I've seen your questions recently and suggest you focus on more simpler programming concepts.

    No offence intended, we all have to start somewhere. No point in trying to drive an F1 car when you've just started learning to drive.
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Coroutines Are not really for a uC microcontroller
     

Share This Page