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

preemptive multitasking possibility

Started by hasunwoo, 09 December 2014 - 12:42 PM
hasunwoo #1
Posted 09 December 2014 - 01:42 PM
simple question. is this possible to implement preemptive multitasking like multishell api in computercraft using coroutine?
i know cooperative multitasking is possible using coroutine.
Bomb Bloke #2
Posted 09 December 2014 - 01:48 PM
Multishell is all Lua (as is the window API it relies on), and indeed works by way of co-routines. It's perfectly possible to script up an equivalent system.
Lyqyd #3
Posted 09 December 2014 - 03:59 PM
No, preemptive multitasking is not readily doable with Lua. You'd have to write an interpreter (in Lua) with the feature, then run all of your scripts through that. It would be much, much slower for very little (if any) gain.

The better question is, why do you think you want preemptive multitasking? There's probably a better/easier way to do what you're trying to accomplish.
Yevano #4
Posted 09 December 2014 - 09:24 PM
Lyqyd is right. Lua has no support for preemptive multitasking. However, the part it's missing is just the task switcher (the part which interrupts the running thread and moves to another one). If you were able to inject task switching code into a target function, you could in theory do what you want. I only mention it because it's something I'm planning to implement for a project I'm working on. If you'd like to hear about it, let me know. Otherwise, I won't pollute the topic with a bunch of technical mumbo jumbo. :P/>
hasunwoo #5
Posted 10 December 2014 - 10:21 AM
Lyqyd is right. Lua has no support for preemptive multitasking. However, the part it's missing is just the task switcher (the part which interrupts the running thread and moves to another one). If you were able to inject task switching code into a target function, you could in theory do what you want. I only mention it because it's something I'm planning to implement for a project I'm working on. If you'd like to hear about it, let me know. Otherwise, I won't pollute the topic with a bunch of technical mumbo jumbo. :P/>
so how does multishell works?
i know multishell do multitasking without native help
please tell me if you know how it works
Bomb Bloke #6
Posted 10 December 2014 - 11:13 AM
Multishell functions as a glorified version of the parallel API - it switches between co-routines whenever they yield. However, those processes specifically have to yield in order for it to work. "Pre-emptive" multitasking lacks this requirement.

But that's fine, as a ComputerCraft script that doesn't yield on a regular basis is going to get killed regardless. This is because much the same system as is used by multishell to switch between scripts, is used by ComputerCraft itself to switch between execution threads on computers - so long as any computer is running code, none of the others are, so if one decides not to yield for an extended time it gets forcefully stopped so that all the computers in the world won't freeze.

That is to say, sure you can build something like multishell, but multishell uses co-operative multitasking - not pre-emptive multitasking.