Thread parameter - struct

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
WaL
Posts: 6
Joined: Tue May 15, 2018 11:52 am
Has thanked: 1 time

Thread parameter - struct

Post 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
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: Thread parameter - struct

Post by rew »

In the thread you need to add:
struct OWStruct *myarg = arg;

and then use "myarg" instead of "arg".
WaL
Posts: 6
Joined: Tue May 15, 2018 11:52 am
Has thanked: 1 time

Re: Thread parameter - struct

Post by WaL »

Thanks, it really work!
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: Thread parameter - struct

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Thread parameter - struct

Post by Giovanni »

Correct, it is there to suppress a warning, if you use the argument then there is no need for it.

Giovanni
Post Reply