Jump to content




Kill a program started with parallel.waitForAll()


  • You cannot reply to this topic
6 replies to this topic

#1 jaredallard

  • Members
  • 124 posts
  • LocationSeattle, WA

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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 December 2014 - 01:17 AM

Why?

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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).

#4 ElvishJerricco

  • Members
  • 803 posts

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 by ElvishJerricco, 11 December 2014 - 10:28 AM.


#5 AgentE382

  • Members
  • 119 posts

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 by AgentE382, 11 December 2014 - 01:16 PM.


#6 ElvishJerricco

  • Members
  • 803 posts

Posted 11 December 2014 - 09:44 PM

View PostAgentE382, on 11 December 2014 - 01:15 PM, said:

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.

#7 AgentE382

  • Members
  • 119 posts

Posted 12 December 2014 - 02:32 AM

View PostElvishJerricco, on 11 December 2014 - 09:44 PM, said:

View PostAgentE382, on 11 December 2014 - 01:15 PM, said:

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 by AgentE382, 12 December 2014 - 02:42 AM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users