Thread: Not consistent coroutines code results

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84

    Not consistent coroutines code results

    Hi,

    I was learning about coroutines and I like it beside I did a similar code using void pointers which contain a lot of casting, so I for now like the coroutines more.

    I did a small example on desktop compiler.
    Codeblocks, 64-bit machine, running on Windows 10.

    This is the code parts.

    1. Example functions:


    Code:
    bool function1(void){
        BeginCoroutine;
        printf("\n1 part1\n");
        co_delay(500);
        printf("\n1 part2\n");
        co_delay(500);
        EndCoroutine;
    }
    
    
    bool function2(void){
        BeginCoroutine;
        printf("\n2 part1\n");
        co_delay(500);
        printf("\n2 part2\n");
        co_delay(500);
        EndCoroutine;
    }
    
    
    
    
    // in function5 and function6, I on purpose doing one delay
    bool function5(void){
        BeginCoroutine;
        printf("\n5 part1\n");
        co_delay(500);
        printf("\n5 part2\n");
        EndCoroutine;
    }
    
    
    bool function6(void){
        BeginCoroutine;
        printf("\n6 part1\n");
        co_delay(500);
        printf("\n6 part2\n");
        EndCoroutine;
    }

    2. Coroutines header:


    Code:
    //////////////////////////////////////////////////////////////////////////////////
    // definitions
    
    
    #define START_TASK_COUNTER(c) task_counter = c;
    
    
    #define BeginCoroutine  static int state = 0;                               \
                            switch(state)                                       \
                            {                                                   \
                                case 0:
    
    
    #define co_delay(prd)	do												    \
                            {												    \
    							static unsigned long st_millis = 0;			    \
                                for(st_millis = millis();millis() - st_millis < prd;)\
                                {                                               \
    								do			                                \
    								{			                                \
    									state = __LINE__;			            \
    									return Yielding;                        \
    									case __LINE__:			                \
    									;           			                \
    								}while (0);			                        \
                                }			                                    \
                            }			                                        \
                            while (0)
    
    
    #define EndCoroutine    }                                                   \
                            task_counter--; 					            	\
                            printf("task %d finished\n",task_counter); 		\
                            state = 0; 					            			\
                            return Done
    
    
    
    
    //////////////////////////////////////////////////////////////////////////////////
    // enumerations
    typedef enum { Done, Yielding } RoutineState;
    
    
    // variables
    uint8_t task_counter;



    3. Coroutines source:


    Code:
    unsigned long millis(void){
        unsigned long t_val;
        t_val = GetTickCount();
        return t_val;
    }

    4. main


    Code:
    void coroutine1(void){
        START_TASK_COUNTER(2);
        printf("\ncoroutine1 -------------------------------\n");
        printf("tasks counts# %d\n",task_counter);
        while(task_counter)
        {
            function1();
            function2();
        }
    }
    
    
    void coroutine2(void){
        START_TASK_COUNTER(2);
        printf("\ncoroutine2 -------------------------------\n");
        printf("tasks counts# %d\n",task_counter);
        while(task_counter)
        {
            function5();
            function6();
        }
    }
    
    
    int main(){
    
    
        RunCoroutine(coroutine1);
        RunCoroutine(coroutine2);
    
    
        return 0;
    }

    =========================================

    Now here 2 console outputs, one is perfect and the next one is different.

    Example console output:


    run#1: Perfect run time, no extra executions and each function part is in order:


    Code:
    
    coroutine1 -------------------------------
    tasks counts# 2
    
    
    1 part1
    
    
    2 part1
    
    
    1 part2
    
    
    2 part2
    task 1 finished
    task 0 finished
    
    
    coroutine2 -------------------------------
    tasks counts# 2
    
    
    5 part1
    
    
    6 part1
    
    
    5 part2
    task 1 finished
    
    
    6 part2
    task 0 finished



    run#2: Here, note that
    coroutine1
    has executed
    2 part1
    again, after the coroutine should be finished:


    Code:
    
    coroutine1 -------------------------------
    tasks counts# 2
    
    
    1 part1
    
    
    2 part1
    
    
    1 part2
    
    
    2 part2
    task 1 finished
    task 0 finished
    
    
    2 part1
    
    
    coroutine2 -------------------------------
    tasks counts# 2
    
    
    5 part1
    
    
    6 part1
    
    
    5 part2
    task 1 finished
    
    
    6 part2
    task 0 finished




    What could be the problem ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A meandering back story to this began many weeks ago here -> Malloc To Pointer Of Void And Assign Different Data Types - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Yes, that's my post in that forum.

    As the thread got really long and there's only one member who is helping me and the member helped me a lot so far and I still have issues in the code, but I learned a lot in that post.

    But I don't want to rely on that member, because I think he/she helped me a lot and you can notice no other members are supporting that thread.

    What do you think ? Is posting here not suitable ? If not, it's OK to delete it and I can continue there.
    Last edited by wolfrose; 05-18-2021 at 05:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. same code different results on different platforms
    By davidkay in forum C Programming
    Replies: 3
    Last Post: 05-24-2013, 09:08 AM
  2. Different results from same code
    By aesalus1976 in forum C Programming
    Replies: 5
    Last Post: 05-31-2012, 03:31 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM

Tags for this Thread