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

Having one thread print while another reads console input

Started by glopso, 13 July 2012 - 04:42 AM
glopso #1
Posted 13 July 2012 - 06:42 AM
What would happen if I called print after calling read on a different thread?
Grim Reaper #2
Posted 13 July 2012 - 06:58 AM
ComputerCraft and Lua, as far as I know, doesn't support multithreading but rather you can take advantage of the parallel API which implements coroutines to mimic simultaneous execution.

Using 'parallel.waitForAny(print("TEST", read()))' would result in the print statement being executed first and then the read would be executed.

Hope I helped! :)/>/>
glopso #3
Posted 13 July 2012 - 07:01 AM
So I can't print something while trying to read()?
MysticT #4
Posted 13 July 2012 - 05:26 PM
Yes, but it wouldn't be at the same time. It looks like it's running at the same time but it's not.
You can use:

local function printStuff()
  while true do
	print("Some text here")
  end
end

local function getInput()
  while true do
	local input = read()
	-- do something with input
  end
end

parallel.waitForAny(printStuff, getInput)
But you have to take care of the functions not overwriting each other's text.