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

Kill a program started with parallel.waitForAll()

Started by jaredallard, 10 December 2014 - 11:29 PM
jaredallard #1
Posted 11 December 2014 - 12:29 AM
Let's say we have two loop programs

a.lua
b.lua

both do

while (true) do
os.sleep(0)
end

Now, let's say we had a third that could be used too stop any one of those a, or b programs started.

How could I stop the co routine created by the parallel API without it successfully returning.
Lyqyd #2
Posted 11 December 2014 - 01:17 AM
Why?
Bomb Bloke #3
Posted 11 December 2014 - 02:06 AM
Short answer is, you don't - that's not how the parallel API's functions are built to work.

You could write similar versions that have that functionality, but depending on what your actual goals are, I'd say it's likely easier to rig your a/b functions so that they can kill themselves (eg, in response to an event fired by your third function).
ElvishJerricco #4
Posted 11 December 2014 - 10:26 AM
Well you can always get dirty with silly awful hacks. In the exact scenario of each loop just doing sleep(0), you can overwrite sleep to error, yield, then rewrite it back to the original.

But yea that's awful. You're going to need to either not use the parallel API in favor of a custom coroutine management system, or you're going to need the functions that are looping to listen for an event or check a status variable or something along those lines.
Edited on 11 December 2014 - 09:28 AM
AgentE382 #5
Posted 11 December 2014 - 01:15 PM
The best hack is to override coroutine.status, because the parallel API uses it to check whether a coroutine is still running. So, you could make a kill function like this:
local routinesToKill = {}
local installed
function kill(routine)
  routinesToKill[routine] = true
  if not installed then
	local oldStatus = coroutine.status
	coroutine.status = function(routine)
	  return routinesToKill[routine] and "dead" or oldStatus(routine)
	end
	installed = true
  end
end
That should work, though I just wrote it. Completely untested.
Edited on 11 December 2014 - 12:16 PM
ElvishJerricco #6
Posted 11 December 2014 - 09:44 PM
The best hack is to override coroutine.status, because the parallel API uses it to check whether a coroutine is still running. So, you could make a kill function like this:
local routinesToKill = {}
local installed
function kill(routine)
  routinesToKill[routine] = true
  if not installed then
	local oldStatus = coroutine.status
	coroutine.status = function(routine)
	  return routinesToKill[routine] and "dead" or oldStatus(routine)
	end
	installed = true
  end
end
That should work, though I just wrote it. Completely untested.

I don't think he'd have access to the routines though. At least not without calling coroutine.current() from within the threads, which seems to go against the goal of not needed the threads to do anything on their own.
AgentE382 #7
Posted 12 December 2014 - 02:32 AM
The best hack is to override coroutine.status, because the parallel API uses it to check whether a coroutine is still running. So, you could make a kill function like this:
local routinesToKill = {}
local installed
function kill(routine)
  routinesToKill[routine] = true
  if not installed then
	local oldStatus = coroutine.status
	coroutine.status = function(routine)
	  return routinesToKill[routine] and "dead" or oldStatus(routine)
	end
	installed = true
  end
end
That should work, though I just wrote it. Completely untested.

I don't think he'd have access to the routines though. At least not without calling coroutine.current() from within the threads, which seems to go against the goal of not needed the threads to do anything on their own.
Hmm… Tru dat.

I was thinking he'd have access to the routines, since he created them. Obviously, I was mistaken. I forgot how the parallel API works for a bit. If he wants this functionality, he's gonna' have to look at a total replacement of the parallel API… one which allows routine naming / killing.
Edited on 12 December 2014 - 01:42 AM