How to Stop a Thread

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

How to Stop a Thread

Post by Bannana97 »

Since :kill() was removed, how can we stop threads now?
I am using threads for hosting servers within the application, and all is fine, except I need to be able to stop the server.

Let me know,
thanks!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to Stop a Thread

Post by bartbes »

You tell it to, and you handle it.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: How to Stop a Thread

Post by Inny »

To expand on bartbes answer, you use Thread:set to post a message that the other thread will act on. For instance:

Code: Select all

-- in main.lua (or in the main thread somewhere)
function kill_thread( thr )
  -- <bartbes> You tell it to
  thr:set("control", "kill")
end

Code: Select all

-- other.lua (the other thread running)
self_thread = love.thread.getThread()

while true do
  local message = self_thread:get( "control" )
  if message == "kill" then
    -- <bartbes> and you handle it
    break
  end 
  love.timer.sleep(1) -- just so we don't waste CPU.
end
If you're designing your threads to be long lived (meaning they're workers, part of a thread pool, or producers), then it's wise to design them around this kind of message-response loop.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: How to Stop a Thread

Post by Luke100000 »

But when the other thread is getting stuck? The thread must be killed from outside.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to Stop a Thread

Post by Nixola »

Luke100000 wrote:But when the other thread is getting stuck? The thread must be killed from outside.
What do you do when the main thread gets stuck? You just kill the whole program as well
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Re: How to Stop a Thread

Post by EliterScripts »

So all of the solutions mentioned on the forums here no longer work, as you cannot use Thread:set(), nor Thread:kill() anymore. I would love to code safety measures, but I can't seem to think of any way that I can make sure a thread is safe. Let's say that on one particular line of code that is executed in Lua makes the thread come to a halt, as it takes say an hour to complete the one line of code. I don't know how to program regular Lua to terminate the chunk or something like that. I know that I can program safeties if there is an error, but if there is no error, and it just keeps going on, there's a problem.

Let's say that the line that is "sleep(3600)" is the example in which there is malicious code, inside of the code of the thread. Is there some sort of way to execute code, pause it, check if everything is alright (i.e. did it take longer than it should have? Is there a request in a set channel to kill the thread?), then resume it? Like, I imagine programming a timeout for 0.5 seconds to do the checks would be reasonable. If code cannot be done within 0.5 seconds, it pauses it, does a check, then resumes it.

Code: Select all

function do_a_thing()
	sleep(3600)
end

function check_myself()
	if(my_channel:pop() == "kill")then
		return false
	else
		return true
 	end
end

local running = true

while(running)do
	local done = false
	while(not done)do
		if(check_myself() == false)then
			running = false
			break
		end
		done = execute_pause_after_timeout(do_a_thing, 0.5) --return false if timed out before it finished
	end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 215 guests