|
Michael J Ash wrote:
> > pid1 = execi (&bumper_task, 0, NULL, PRIO_NORMAL+4,
> > DEFAULT_STACK_SIZE);
> > pid2 = execi (&left_task, 0, NULL, PRIO_NORMAL+2,
> > DEFAULT_STACK_SIZE);
> > pid3 = execi (&right_task, 0, NULL, PRIO_NORMAL,
> > DEFAULT_STACK_SIZE);
> > while( 1 ); // <<<<<<<<<<<<< this is crucial!
>
> I would strongly suggest changing this to while(1) msleep(bignumber);.
> Otherwise you're hogging the processor doing nothing important, which
> could do bad things to any compute-intensive tasks you may have going.
Of course you are right. I think it is a bad programming style not to give
your program the ability to terminate but to run in an endless loop. But
what is the penalty for this bad style? I've done some investigations and
here are the results:
I sat up a working task doing some silly calculations and measuring the
time for them. I examined the disassembly to be sure that the loop isn't
optimzied out by the compiler.
-------------------------- cut here --------------------------------------
#include <conio.h>
#include <unistd.h>
pid_t worker;
int fld[256];
int work_task(int argc, char *argv[]);
int main()
{
worker = execi (&work_task, 0, NULL, PRIO_NORMAL+4, DEFAULT_STACK_SIZE);
while( 1 );
return 0;
}
int work_task (int argc, char *argv[])
{
int i;
unsigned start_time, end_time;
start_time = (unsigned int)sys_time;
for( i = 1; i < 15000; i++ )
fld[i&255] = 3 * i + i * i + 9;
end_time = (unsigned int)sys_time;
lcd_int( end_time - start_time );
return 0;
}
------------------------ cut here ------------------------------------------
This program gives a time of 827. And it doesn't matter what we do with the
while-statement!!! Doing a
while( 1 );
while( 1 ) yield();
while( 1 ) sleep( 60 );
changes nothing.
This is, because the work_task thread has a priority of NORMAL+4. So, the
main task won't get any time slice at all.
Things get different, if we give both threads (main and work_task) the same
priority. ( i.e. PRIO_NORMAL in the execi() ). Now we get:
while( 1 ); 1648
while( 1 ) yield(); 835
while( 1 ) sleep( 60 ); 827
With while(1) both threads get a time slice each, the main thread does
nothing and the work thread needs about 2 times the time used running
alone. Yield() is not as efficient for giving up time as sleep(), but helps
a lot, too.
So what is the beef? If you do a math intensive task, giving it high
priority makes it the only task running. If you depend on "real"
multitasking, it would be better to give them the same priority.
|
|
Message has 1 Reply: | | Re: More on threads
|
| (...) ability to terminate but to run in an endless loop. But This is not true of an embedded system though which will have to run 'forever'. (...) priority of NORMAL+4. So, the (...) any time slice at all. NO time slice or just a very small chunk (...) (23 years ago, 19-Jan-02, to lugnet.robotics.rcx.legos)
|
Message is in Reply To:
| | Re: threads - bug in the firmware?
|
| (...) I would strongly suggest changing this to while(1) msleep(bignumber);. Otherwise you're hogging the processor doing nothing important, which could do bad things to any compute-intensive tasks you may have going. It'll also use slightly less (...) (23 years ago, 18-Jan-02, to lugnet.robotics.rcx.legos)
|
18 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|