Shop OBEX P1 Docs P2 Docs Learn Events
In C, multicore function declaration question — Parallax Forums

In C, multicore function declaration question

twm47099twm47099 Posts: 867
edited 2014-01-02 17:13 in Learn with BlocklyProp
In the tutorial C - Functions Multicore Example, the following is used to declare and define the function that is used in the new cog:

void adder(void *par);

I've looked through some of the c-libraries and see the same parameter when starting a function in a new cog. From reading my C text (void *par) is an untyped pointer to par. What does that do in relation to the function in the new cog? Is par a reserved variable name that is used any time a function is started in a different cog? In other words, if I am starting a different function in each of 3 cogs, would I use (void *par) as the parameter for each function?

Related question: Can I pass other parameters to a cog in a function call? If so what does the declaration of the function look like, and is void *par still one of the parameters?

Thanks again
Tom

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-12-29 14:03
    In "void adder(void *par);" the par is whatever you want it to be.

    The name par is not special; it's just a declaration. It can be called "MarilynMonroll" if you like - a most interesting Sushi dish in Rocklin.

    Some examples (code has not been test compiled):
    int stack[200/4]; // 200 bytes of stack should be global.
    
    volatile int parnum = 10; // par should be volatile to keep compiler from optimizing away code.
    volatile int pararray[4];
    typedef struct mystruct {
      volatile int request; volatile int response;
    } MyST;
    
    MyST *parstvar;
    
    void adder1(void* num)
    {
      int number = (int) num;
    }
    
    void adder2(void* numptr)
    {
      int number = *(int*) numptr;
    }
    
    void adder3(void* array)
    {
      int n;
      int *myArray = (int*) array;
      for(n = 0; n < 4; n++) {
        myArray[n] = n;
      }
    }
    
    void adder4(void* stptr)
    {
      MyST sp = (MyST*) stptr;
    }
    
    int main(void) {
      cogstart(adder, (void*) parnum, &stack, sizeof(stack)); // passes the value in parnum to the function - not recommended.
      cogstart(adder, &parnum, &stack, sizeof(stack)); // passes the address of parnum to the function.
      cogstart(adder, &pararray[0], &stack, sizeof(stack)); // passes the address of pararray[0] to the function. 
      cogstart(adder, &parstvar, &stack, sizeof(stack)); // passes the address of pararray[0] to the function.
      return 0;
    }
    
    Reference: https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library#TOC-cogstart

  • TymkrsTymkrs Posts: 539
    edited 2014-01-02 17:13
    I believe we go into each line of the code and explain what it is and what can be done with each line if you're interested twm:

    http://www.tymkrs.com/shows/episode/firstc-episode-011/
Sign In or Register to comment.