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

Computer reboots in big loop.

Started by RefinedCode, 02 August 2012 - 04:10 PM
RefinedCode #1
Posted 02 August 2012 - 06:10 PM
Hi guys i have the following code:


for i = 1,attempts do
	 network:backwardPropagate({0,0},{0})
	 network:backwardPropagate({1,0},{1})
	 network:backwardPropagate({0,1},{1})
	 network:backwardPropagate({1,1},{0})
end

In this situation attempts = 10000.

WHen i run it just like that my computer just reboots without finishing the program.

If i add sleep(.1) it runs fine, but it take 16 minutes longer than it should (because .1 *10000 = 1000 seconds = 16 minutes)

Is there anything i can do to avoid the sleep and cut down the run time?

Does anyone know why the program would be rebooting the computer?

Thanks!
MysticT #2
Posted 02 August 2012 - 06:45 PM
The problem is that the program needs to yield, or ComputerCraft will shutdown the computer. That's just to prevent infinite loops that don't let anything else run.
sleep calls os.pullEvent, wich yields to wait for an event, that's why it work when you add it. If you don't want it to wait, you can queue a fake event so you continues right after yielding. This should work:

local function yield()
  os.queueEvent("somefakeevent")
  os.pullEvent("somefakeevent")
end
Just add a call to that function inside at the end of the loop.
RefinedCode #3
Posted 02 August 2012 - 07:06 PM
Awesome! Thanks so much.
Kolpa #4
Posted 02 August 2012 - 07:45 PM
The problem is that the program needs to yield, or ComputerCraft will shutdown the computer. That's just to prevent infinite loops that don't let anything else run.
sleep calls os.pullEvent, wich yields to wait for an event, that's why it work when you add it. If you don't want it to wait, you can queue a fake event so you continues right after yielding. This should work:

local function yield()
  os.queueEvent("somefakeevent")
  os.pullEvent("somefakeevent")
end
Just add a call to that function inside at the end of the loop.
well you can also do it the easy way and sleep for 0 seconds
sleep(0)
MysticT #5
Posted 02 August 2012 - 08:30 PM
well you can also do it the easy way and sleep for 0 seconds
sleep(0)
Yes, but it's faster using just yield, since it won't create a timer and wait for it to trigger (even when it's set to 0). However, the difference wouldn't be really much, so you may not even notice, so you can use any of them :ph34r:/>/>.
Kolpa #6
Posted 02 August 2012 - 09:24 PM
well you can also do it the easy way and sleep for 0 seconds
sleep(0)
Yes, but it's faster using just yield, since it won't create a timer and wait for it to trigger (even when it's set to 0). However, the difference wouldn't be really much, so you may not even notice, so you can use any of them :ph34r:/>/>.
true that i suppose :)/>/>