This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ThiagoTGM's profile picture

Parallel/sleep Problem

Started by ThiagoTGM, 18 October 2013 - 05:25 PM
ThiagoTGM #1
Posted 18 October 2013 - 07:25 PM
Hello everyone.

I have an program made to get values on redstone energy cells and display some data about it. It also displays, and controls, energy production lines for each cell. Now, I was trying to make the On/Off indicator for each cell on the monitor display a small type of animation (simply write everything slowly) when production for that cell. However I have now an ridiculous amount of input lag (up to 1sec lag is normal, because of how I handle the loop), and I'm not sure if the problem is related to my use of either the Parallel API or the Sleep() function. I'm looking for some help.

The program: http://pastebin.com/mWxh9Lmk
Yevano #2
Posted 18 October 2013 - 07:54 PM
At a glance, I suspect that the lag is because you aren't running the input in the parallel call. This means that input has to wait until all the animations finish, as well as your timer. If you ever have to sleep to prevent busy waiting (such as inside your timer function), you're usually doing something wrong. Although I'm not entirely sure if you're doing the sleep to prevent busy waiting.

If you need to have different threads run once a part of another thread finishes, you can queue an event for the other thread to catch. This way, the threads establish an execution relationship and none of them will try to run without the others being ready yet.
ThiagoTGM #3
Posted 18 October 2013 - 08:43 PM
At a glance, I suspect that the lag is because you aren't running the input in the parallel call. This means that input has to wait until all the animations finish, as well as your timer. If you ever have to sleep to prevent busy waiting (such as inside your timer function), you're usually doing something wrong. Although I'm not entirely sure if you're doing the sleep to prevent busy waiting.

If you need to have different threads run once a part of another thread finishes, you can queue an event for the other thread to catch. This way, the threads establish an execution relationship and none of them will try to run without the others being ready yet.

Actually the input reciever is inside the Timer function. And the sleep() functions are only in the animation, to make it slow down. Lag up to one second is normal, as I made it that way so the refresh rate is always one sec (as to make the variation values as precise as possible).
immibis #4
Posted 19 October 2013 - 04:37 AM
I don't know if this causes your problem, but:

sleep is written something like this:

function sleep(seconds)
  local timerID = os.startTimer(seconds)
  repeat
    local event, param = os.pullEvent("timer")
  until param == timerID
end

Notice how sleep creates a timer? And your timer function finishes when *any* timer goes off. It should check if the timer that went off is the one it set.