Page 1 of 1

Thread suspend

Posted: Wed Feb 07, 2018 12:03 pm
by omcdr
How to suspend task ?
I have simple code, one thread, I start it and call chThdSuspendS in this task - it works.
I can resume with chThdResume this thread, but I can't suspend it. I call chThdSuspendS in main, it return MSG_OK, but thread still working.
What is wrong ?


Code: Select all

typedef struct trg_t
{
   thread_t* threadp;

} trg_t;

THD_WORKING_AREA(waThdtrg, CONFIG_trg_THD_WA_SIZE);
THD_FUNCTION(trgThd, arg)
{
   trg_t *trg = arg;

   //do something
   
   chSysLock();
   chThdSuspendS(&(trg->threadp)); //suspend this task
   chSysUnlock();

   while (1)
   {
   // do in while

      chThdSleepMilliseconds(10);
   }
}

In main

   trg.threadp = chThdCreateStatic(waThdtrg, sizeof(waThdtrg), CONFIG_trg_THD_PRIORITY, trgThd, (void *) &trg);

   while(1)
   {   
      if ( (...) && trg.threadp->state == CH_STATE_SUSPENDED)
         chThdResume(&(trg.threadp), (msg_t) 0);
      else if(...)
      {
         msg_t x;
         chSysLock();
         x = chThdSuspendS(&(trg.threadp));
         chSysUnlock();
      }
   }

Re: Thread suspend

Posted: Wed Feb 07, 2018 1:55 pm
by Giovanni
The reference to the thread must be initialized to NULL, this is the problem I can see.

Enable checks, assertions etc.

Giovanni

Re: Thread suspend

Posted: Thu Feb 08, 2018 3:03 pm
by omcdr
How to suspend task, if I know thread_t * ?
Can I from main suspend other task ? chThdSuspendS can suspend only current thread.

Re: Thread suspend

Posted: Thu Feb 08, 2018 3:11 pm
by Giovanni
You can't suspend other tasks, by design.

Giovanni