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

[1.53][SMP]parallel.waitForAny() runs first function only

Started by Pyro_, 10 June 2014 - 10:06 AM
Pyro_ #1
Posted 10 June 2014 - 12:06 PM
Hi all,

Firstly, please don't lynch me for using such an outdated version of CC, it's not up to me, it's up to the mod pack creator, and they want to stay at this version for compatibility with other mods (yeah, I know).
Secondly, I can't get the file off the server, so I can't pastebin it here for you to look at (unless I'm just stupid, then please, let me know)

Anyway,

I have all of my code in separate functions and two functions with while true loops, like so:


local function something()
end

local function somethingElse()
end

...

local function drawDynamic()
  while true do
	all of the things
  end
end

local function updateExternal()
  while true do
	all of the other things
  end
end

parallel.waitForAny(drawDynamic(), updateExternal())

but it seems that whenever I run the program, only the function listed first in the waitForAny() function call parameters gets run. For example, if I have the order of the parameters as:


parallel.waitForAny(drawDynamic(), updateExternal())

In the above case, drawDynamic() will run, but updateExternal() never seems to run, and if I reverse the order:


parallel.waitForAny(updateExternal(), drawDynamic())

Then in this case updateExternal() will run, but drawDynamic() does not seem to run.

Is this a bug with this version, or with the particular implementation, or with my use of the api? And is there a workaround or a fix I could use to get this to work? (Short of using multiple computers :)/> )

Thanks
CometWolf #2
Posted 10 June 2014 - 12:25 PM
Chances are you misunderstand how the parallel API works, which seems to sadly be very very common… No CC program will ever run 2 threads simoultaneously, in fact no 2 CC computers will ever run simoultaneously in the same world. Essentially what parallel does, is run the first function, untill it calls coroutine.yield, then it runs the second function until it does the same. Afterwards, it waits for whatever event type the functions requested, then resumes the functions in order if the event they requested occured, passing said event parameters to it. Note that os.pullEvent, os.pullEventRaw, sleep, turtle functions and rednet.recieve all call coroutine.yield. We can't really provde any further assistance regarding your problem without seeing your code, but chances are the error is on your end.


Secondly, I can't get the file off the server, so I can't pastebin it here for you to look at (unless I'm just stupid, then please, let me know)
You realize you can upload it to pastebin directly from the game, using the built in pastebin program… right?
theoriginalbit #3
Posted 10 June 2014 - 12:27 PM
Okay so this is a perfect thread for you, particularly my reply.
Pyro_ #4
Posted 10 June 2014 - 12:32 PM
Okay, thanks bit. I was hoping that they might actually invoke their own threads on the host PC. This makes my life a little bit more difficult…

Edit: Upon closer inspection, it seems that it might be alright in the end anyway. Thanks for your help bit.
Edited on 10 June 2014 - 10:41 AM