Recommended way to exit and create threads

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

Moderator: RoccoMarco

Post Reply
wdsouza
Posts: 2
Joined: Mon Nov 14, 2016 7:04 am

Recommended way to exit and create threads

Post by wdsouza »

Hello all,

I have 2 threads which exit themselves and are created by the other, like so:

Code: Select all

static THD_FUNCTION(Thread6, arg)
{
  (void)arg;
  chRegSetThreadName("6");
 
  /*
  * Wait for some error condition to occur
  */
 
  chThdCreateStatic(waThread3, sizeof(waThread3), 62, Thread3, NULL );
 
  chThdExit(0);
}

static THD_FUNCTION(Thread3,arg)
{
  (void)arg;
  chRegSetThreadName("3");
 
  /*
  * Execute some functions
  */
 
  chThdCreateStatic(waThread6, sizeof(waThread6), 50, Thread6, NULL );
  chThdSleepMilliseconds(1000);

  chThdExit(0);
}

void main()
{
  chThdCreateStatic(waThread3, sizeof(waThread3), 62, Thread3, NULL );
  while(true)
  {
    chThdSleepMilliseconds(100);
  }
}


In chconf.h
#define CH_CFG_USE_REGISTRY TRUE
#define CH_CFG_USE_WAITEXIT TRUE


Is this a recommended way to exit and create the threads?

Thanks.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Recommended way to exit and create threads

Post by Giovanni »

Hi,

Yes, it is the recommended way. Are you sure you need two threads there?

Giovanni
wdsouza
Posts: 2
Joined: Mon Nov 14, 2016 7:04 am

Re: Recommended way to exit and create threads

Post by wdsouza »

Hello Giovanni,

Thanks for the reply.

The above is just an example.

My application has to spawn threads after a particular condition is true. The spawn threads themselves then wait for a condition, does a process depending on the condition and then exits. And the process then continues.

Don't know if this is a issue per say, after a thread has exited and created a random number of time. The shell command "threads" goes in an infinite loop display.

Check thread name 42, First time it is created and exited the shell command "threads" displays.
threads_1.png


After the random number of creation and exits the shell command "threads" displays.
threads_2.png


Thanks.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Recommended way to exit and create threads

Post by Giovanni »

That could be a registry corruption.

What could happen is that one thread creates the other one but is preempted before performing its exit, then it is recreated again while it is still active.

Your should perform and atomic create and exit using I and S functions.

Giovanni
Post Reply