AW: [En-Nut-Discussion] NutThreadSetPriority

Oliver Schulz Oliver.Schulz at bong.de
Fri Dec 5 13:00:16 CET 2003


Hi Yuri,

lets start to examine your code and answer your questions...

> 
> for (i = 0; i < 4; i++) {
>        char *thname = "main";
>        thname[4] = '0' + i;
>        NutThreadCreate(thname, NutMain, (void *) (u_short) i, 640);
>        printf("[%i] Name: %s\r\n", i, tdp->td_name);
>        }
> NutThreadSetPriority(255);
> printf("[%i] Idle Thread Priority: %u\r\n", i, tdp->td_priority);

1. If you set the thread number with "thname[4] = '0' + i;" you overwrite the string termination character! thname is prior set to "main", so it has 4 characters [0..3] and one termination char [4]. If you set thname to "main0" for example, the termination char is on index [5] and will not be overwritten.

2. You MUST assign the return value from NutThreadCreat to tdp! You don't, so tdp points somewhere into nirvana.. Please use "tdp = (NUTTHREADINFO*) NutThreadCreate (...);"

3. If you try to print the idle thread priority, tdp points still to the last created thread. This will not work. Use "runningThread->td_priority" instead. But note, calling NutThreadSetPrio(255) will immediately suspend your idle thread and it executes printf first after it becomes active again..

> maybe im just being ignorant, but i dont understand what effect
> NutThreadYield() has in an endless for loop. Because when the priority
> gets set so low, the thread loses control over itself, thus, the way i
> see it, isnt capable of forfilling his task by Yielding the thread...
> maybe somebody can explain this a bit more.

If all other threads are waiting for something (in most cases for timers or other events) the idle thread becomes active. Just imagine that all your "worker"-threads are waiting for a tcp connection...
If than the idle thread does not call NutThreadYield(), it will never suspended and all other thread have no chance ever to get to run.


Hope that helps,
Oliver.



More information about the En-Nut-Discussion mailing list