needed = true;
function toggle()
needed = true;
end
function main()
registerTimer("timer", 5000, toggle);
printf("Hello World!\n");
printf("Press SPACE BAR to terminate script.\n");
while(1) do
-- if they press space bar, break out of the while
if( keyPressedLocal(key.VK_SPACE) ) then
break;
end
if( needed ) then
printf("needed!\n");
yrest (1000);
needed = false;
yrest(1000);
end
end
printf("Goodbye!\n");
end
-- startMacro will now call main() within a protected environment for us.
startMacro(main);
He should print the given line every 5 seconds, but he prints it only one time.
Yeah, the timer processing will only be done while yielding. This is intentional (prevents potentially 'long' timer functions from interfering with time-critical tasks). Even a yrest(1) will do the trick (and prevent your script from using 100% of a core on the CPU).
Administrator wrote:Yeah, the timer processing will only be done while yielding. This is intentional (prevents potentially 'long' timer functions from interfering with time-critical tasks). Even a yrest(1) will do the trick (and prevent your script from using 100% of a core on the CPU).
guys thank you so much. works perfectly now!
now that i know what was missing, i want to understand WHY yrest() is so important. i get the point that i create some time, allowing the script to process other things.
how can i measure how much time i must set yrest? when and where in the script should it be inserted? what exactly means "yielding" ? (i'm german, my dictionary didnt help xD)
It's very simple.
Yielding is simply allowing something to go first.
Imagine two busy lanes in a road merging into one. If the drivers in the right lane never yielded for traffic in the left lane, the left lane would stop moving. Now imagine that every 10nth car in the right lane yielded for a car in the left lane. Only a tenth as many cars in the left lane would merge. The perfect scenario is that ever car in the right lane yielded to allow one car in the left lane ahead of them.
Multi-threading is the act of two separate sets of code trying to run at the same time on one processor.
Multi-threading principles are exactly the same. But instead of cars, we are talking lines of code and instead of lanes we are talking about the processor. Thereby, if you want to run two separate pieces of code utilizing one processor, each set of code needs to perform some amount of work and then yield for the other piece of code so it to can use the processor.
It's hard to say when you should yield. You have to ask yourself, how accurate do you want your timer. Do you need it to run EXACTLY on time? Then you need to add more yields. Or can you let it slip a second, then you can Yield less.
It's probably not wise to yield every other line. Just yield after your code has performed some amount of work.
how can i measure how much time i must set yrest? when and where in the script should it be inserted? what exactly means "yielding" ? (i'm german, my dictionary didnt help xD)
Just yield inside loops and such. That's the most important thing. You don't have to worry about timing it yourself (unless you specifically want to rest for 1 second or something like that). Just call yrest(1) to allow other coroutines a change to do their processing and they will return control back when they are done.