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

Lua "Multitasking"

Started by H4X0RZ, 16 May 2014 - 09:09 PM
H4X0RZ #1
Posted 16 May 2014 - 11:09 PM
Hello community,
I read a post about "How do I creat an OS?" and there was a wonderful answer by @apemanzilla. He/She talked about that an OS isn't just a GUI and a collection of programs. In his opinion an OS will need "multitasking" (There is no real multitasking in Lua for those who don't know) also. He/She said that the parallel API is too limitied also.

So I decided to start writing my own "multitasking" API. I would wonder if what I did would work, so I want to ask if anyone could "take an eye on it" and help to debug it.
Here is the code: http://pastebin.com/QacNTUW9

It would really help me if anyone of you could help me with this messy piece of code :)/>

~Freack100
Konlab #2
Posted 17 May 2014 - 09:11 AM
Good idea, I think its working, haven't tested yet, but its cool.
H4X0RZ #3
Posted 17 May 2014 - 10:56 PM
No one can help me?
Yevano #4
Posted 17 May 2014 - 11:15 PM
No one can help me?

We can't help you if we don't know if anything is wrong with it. It seems like you're trying to make something similar to Gopher's goroutines, or my SEE threads (I'm sure there are tons of others). From the peek I took, it looked like you have the basic idea down. Inner event loop which resumes all running task and keeps track of their event filters. It's basically identical to the parallel API except that you can add new threads to the event loop at any time.
Edited on 17 May 2014 - 09:16 PM
augustas656 #5
Posted 17 May 2014 - 11:18 PM
Perhaps, something like java's threading could be implemented in lua's programming functionality. Java's threading is possibly the best multi-tasking functionality.
Yevano #6
Posted 17 May 2014 - 11:22 PM
Perhaps, something like java's threading could be implemented in lua's programming functionality. Java's threading is possibly the best multi-tasking functionality.

For the same multi-tasking as in Java, you'd need to use system threads. Since Lua is not thread-safe, this is not possible unless you run each thread in a separate Lua state. The closest you can get in pure Lua is the system I described above, which uses cooperative multitasking rather than preemptive.