Using until() versus Wait() with multiple tasks.
Posted: 17 Feb 2013, 02:08
I have a program with 4 tasks running. There are times when task_1 is simply waiting on task_2 to complete some
sensing operations. During this time, I want task_1 to use as little CPU time as possible, so task_2 can better monitor a
sensor. If I know that task_2 won't complete the sensing operation for at least 1 second, what code in task_1 will use
less CPU time? In both choices, task_2 sets global_flag_01 = TRUE, when it finishes its sensing operations.
Choice A:
task_1 is doing its work;
it then needs to wait for task_2
Wait(990); // Task_2's sensing operation takes at least 1 second.
until(global_flag_01 == TRUE);
task_1 resumes it work
or Choice B:
task_1 is doing its work;
it then needs to wait for task_2
until(global_flag_01 == TRUE);
task_1 resumes it work
I.e., is a Wait more efficient than an until? Will the until() get executed many times during a task_2's time slice,
where with the Wait(), the scheduler will see that code, and simply not give that task any time slice for a while? Is
this a sensible question, or does the scheduler operate is some manner that doesn't fit my mental picture?
And a variation I may need in my code is, I don't know how long task_2's sensing operation will take, but I want to
minimize task_1's CPU usage while it is waiting, even if it means task_1 may not start immediately when task_2
sets the flag, so I considering Choice B above or Choice C.
Choice C:
task_1 is doing its work;
it then needs to wait for task_2
while (global_flag_01 == FALSE)
{
Wait(50);
}
task_1 resumes it work
And a related question that I wonder about, what is the standard time slice for each task?
Thanks for any ideas.
Howard
sensing operations. During this time, I want task_1 to use as little CPU time as possible, so task_2 can better monitor a
sensor. If I know that task_2 won't complete the sensing operation for at least 1 second, what code in task_1 will use
less CPU time? In both choices, task_2 sets global_flag_01 = TRUE, when it finishes its sensing operations.
Choice A:
task_1 is doing its work;
it then needs to wait for task_2
Wait(990); // Task_2's sensing operation takes at least 1 second.
until(global_flag_01 == TRUE);
task_1 resumes it work
or Choice B:
task_1 is doing its work;
it then needs to wait for task_2
until(global_flag_01 == TRUE);
task_1 resumes it work
I.e., is a Wait more efficient than an until? Will the until() get executed many times during a task_2's time slice,
where with the Wait(), the scheduler will see that code, and simply not give that task any time slice for a while? Is
this a sensible question, or does the scheduler operate is some manner that doesn't fit my mental picture?
And a variation I may need in my code is, I don't know how long task_2's sensing operation will take, but I want to
minimize task_1's CPU usage while it is waiting, even if it means task_1 may not start immediately when task_2
sets the flag, so I considering Choice B above or Choice C.
Choice C:
task_1 is doing its work;
it then needs to wait for task_2
while (global_flag_01 == FALSE)
{
Wait(50);
}
task_1 resumes it work
And a related question that I wonder about, what is the standard time slice for each task?
Thanks for any ideas.
Howard