Page 1 of 1

Thread parameter - struct

Posted: Tue May 22, 2018 7:43 am
by WaL
Hello!
I'm trying to pass parameters in the thread.
I already understood that this requires a structure, but then I stopped

Code: Select all

/*
 * Поток мигания светодиодом
 */
static THD_WORKING_AREA(waThreadBlink, 128);

static msg_t ThreadBlink(void *arg) {

  (void)arg;

  chRegSetThreadName("blinker");

  palSetPadMode(GPIOC, GPIOC_LED, PAL_MODE_OUTPUT_PUSHPULL);

  while (TRUE)
  {
    palTogglePad(GPIOC, GPIOC_LED);
    chThdSleepMilliseconds(arg->time2);
  }
  return (msg_t) 0;
}


Code: Select all

struct OWStruct
    {
      uint32_t timer;
      uint32_t time2;
    }OWStruct;
   


Code: Select all

OWStruct.time2=100;
chThdCreateStatic(waThreadBlink, sizeof(waThreadBlink), NORMALPRIO, ThreadBlink,&OWStruct);


I'm getting a compiler error
error: request for member 'time2' in something not a structure or union

Re: Thread parameter - struct

Posted: Tue May 22, 2018 8:29 am
by rew
In the thread you need to add:
struct OWStruct *myarg = arg;

and then use "myarg" instead of "arg".

Re: Thread parameter - struct

Posted: Tue May 22, 2018 8:41 am
by WaL
Thanks, it really work!

Re: Thread parameter - struct

Posted: Tue May 22, 2018 6:07 pm
by rew
Oh, One more little "neatness" thingy....

the "(void) arg;" is there to tell the compiler: "I know what I'm doing, I declared the argument arg and I'm not going to use it. Please don't warn me about THAT!". Well... that's unnecessary when you ARE using it.

Re: Thread parameter - struct

Posted: Tue May 22, 2018 9:49 pm
by Giovanni
Correct, it is there to suppress a warning, if you use the argument then there is no need for it.

Giovanni