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

ComputerCraft Computer shuts down when multitasking...

Started by greenvbuser, 06 July 2013 - 07:58 AM
greenvbuser #1
Posted 06 July 2013 - 09:58 AM
Title: ComputerCraft Computer shuts down when multitasking…

Hey, I have createt a program that runs two while(true) loops in parallel. But always, after a few seconds (and no errors at all!) the computer (not the real one, but the ComputerCraft Computer) shuts completely down for, in my opinion, no reason.
This is the code, hope you'll understand…

queue = {}
iQueue = 0
jQueue = 0
run = true
function main()
print("Running...")
parallel.waitForAll(processQueue, doQueue)
main()
end
function processQueue()
while(run == true) do
  local a, message, b = rednet.receive()
  queue["iQueue"] = message
  if(iQueue == 16) then
   iQueue = 0
  else
   iQueue = iQueue + 1
  end
end
end
function doQueue()
while (run == true) do
  if(queue["jQueue"] ~= nil) then
   r=unserialize(queue["jQueue"])
   if(r.target == "mww.oil-one.te/secuServer") then
	rednet.sendMessage(2, s)
	print("Forwarding data for "..r.target.." to Terminal '2'")
   elseif(r.target == "mww.oil-one.te/mainentrance") then
	rednet.sendMessage(0, queue["jQueue"])
	print("Forwarding data for "..r.target.." to Terminal '2'")
   end
   if(jQueue == 16) then
	 jQueue = 0
   else
	jQueue = jQueue + 1
   end
  end
end
end
main()

thanks and best regards.
yours, greenvbuser ;)/>

by the way… I do now know that I can't post a new topic… but WHY not? Isn't it a little weird to have a forum where people cannot ask anything?
Cranium #2
Posted 06 July 2013 - 11:05 AM
Split into new topic.

The reason new users can't create topics of their own, is so that we can prevent spammers from filling our boards with useless junk.
albrat #3
Posted 06 July 2013 - 12:32 PM
with your code your CC computer gets a stack overflow as the Main() calls itself from inside itself. so basically your computer crashes .

instead of calling Main() try using Return main()
and see if your computer crashes.

Also I would suggest letting your main() actually close properly and just having the last command "main()" inside a loop for example

while true do
  main()
sleep(0.5)
end
this means each call of main() opens and closes. The sleep is to try and avoid the catchment that says your program is using too much processing time.

NB : if you do the following with rednet.receive(0.8) you no longer need the sleep(0.5)

I also notice that oyu did not set a timeout on the :- "rednet.receive()" this means that the code will pause indefinately and do nothing untill it receives a rednet message.

I suggest trying rednet.receive(0.8) - 1 minute of server time pause to receive a message.
greenvbuser #4
Posted 06 July 2013 - 04:40 PM
Nope, sorry, still seems to crash… but again: not a single error. Do you have any more ideas? Still thanks, though.
Yevano #5
Posted 06 July 2013 - 08:12 PM
Your second loop never calls any yielding functions such as os.pullEvent or rednet.receive. To fix this, you could have the loop wait for a custom event from another part of your code, or you could have it only fire on an interval using sleep. (the former option is most likely a better idea)
greenvbuser #6
Posted 07 July 2013 - 05:54 AM
Yeah, now it works… well, at least it ain't crash anymore. What I wanted it do do does not seem to work though. But now I have something to work with.
Thanks a lot ;)/>