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!
How to Stop a Thread
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: How to Stop a Thread
You tell it to, and you handle it.
Re: How to Stop a Thread
To expand on bartbes answer, you use Thread:set to post a message that the other thread will act on. For instance:
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.
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
- Luke100000
- Party member
- Posts: 232
- Joined: Mon Jul 22, 2013 9:17 am
- Location: Austria
- Contact:
Re: How to Stop a Thread
But when the other thread is getting stuck? The thread must be killed from outside.
Re: How to Stop a Thread
What do you do when the main thread gets stuck? You just kill the whole program as wellLuke100000 wrote:But when the other thread is getting stuck? The thread must be killed from outside.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
-
- Citizen
- Posts: 85
- Joined: Sat Oct 25, 2014 7:07 pm
Re: How to Stop a Thread
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.
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
Who is online
Users browsing this forum: No registered users and 215 guests