Page 1 of 1

Recommended way to exit and create threads

Posted: Mon Nov 14, 2016 7:28 am
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.

Re: Recommended way to exit and create threads

Posted: Mon Nov 14, 2016 9:13 am
by Giovanni
Hi,

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

Giovanni

Re: Recommended way to exit and create threads

Posted: Mon Nov 14, 2016 10:58 am
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.

Re: Recommended way to exit and create threads

Posted: Mon Nov 14, 2016 12:03 pm
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