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

Multithreading Questions

Started by Parmacoy, 23 September 2013 - 07:54 PM
Parmacoy #1
Posted 23 September 2013 - 09:54 PM
Hey, earlier today I started thinking about if a form of program multitasking is possible in Lua / Computercraft.
Essentially, either have a custom OS that has certain file setups, which loads all the files that need to be run, and this is the part I'm not sure is possible. Sequentially go through each of the files, and append each line to a table.

So what I mean is say I start an edit program, then I start a chat monitor or peripheral monitor, two different files, and on load of the files, it splits them up so that say programs[1][500] returns the line string to be ran, then programs[2][500] does the same, so an expandable sequential program runner, i guess sort of like multithreading or the parallel api, but not. Is this possible? also would it be possible if compliled to bytecode, how does that work, tried it out today, and could not get it to work properly.

so not sure how lua interprets code, but in this case

str = "Hello World"
function output(msg)
	print(msg)
end
output(str)

str = "Goodbye World"
function output(msg)
	print(msg)
end
output(str)

So in a sense
program[1][1] = 'str = "Hello World"'
program[1][2] = 'str = "Goodbye World"'
program[2][1] = 'function output(msg)'
program[2][2] = 'function output(msg)'

Thats how Im sort of thinking breaking it down, then its an easy matter to iterate over

Is that possible to be broken down into sequential small steps so the perception of many programs running simultaneously is fluid. Yes i do know about the parallel api and the coroutine functions, but I don't believe this comes under those. I was almost thinking along the lines of machine language, how each step or group of steps is ran, then another programs bit of code can be run, then switch back.
Lyqyd #2
Posted 23 September 2013 - 10:46 PM
Yeah, don't do this. You'd be waaay better off just sticking with the usual coroutine model. If you really want to do this (for fun, since it serves no practical purpose), you'd have to essentially write your own bytecode compiler/interpreter, except based on individual lines of code, which is a rather meaningless unit.
Parmacoy #3
Posted 23 September 2013 - 10:49 PM
Fair enough, thanks for the reply.
Yea was just an idea where I thought "I wonder if this is possible".