love.thread

Available since LÖVE 0.7.0
This module is not supported in earlier versions.

Allows you to work with threads.

Threads are separate Lua environments, running in parallel to the main code. As their code runs separately, they can be used to compute complex operations without adversely affecting the frame rate of the main thread. However, as they are separate environments, they cannot access the variables and functions of the main thread, and communication between threads is limited.

All LOVE objects (userdata) are shared among threads so you'll only have to send their references across threads. You may run into concurrency issues if you manipulate an object on multiple threads at the same time.

When a Thread is started, it only loads the love.thread module. Every other module has to be loaded with require.


Question: Does this means necessarily a gain in terms of speed?

Answer: No, threads never do. That is, they can make it faster, but they can't guarantee it.

(http://love2d.org/forums/viewtopic.php?f=4&t=12031&hilit=Thread#p72039)


O.png The love.graphics and love.window modules have several restrictions and therefore should only be used in the main thread.  


Types

Channel An object which can be used to send and receive data between different threads. Added since 0.9.0
Thread A Thread represents a thread. Added since 0.7.0

Functions

love.thread.getChannel Creates or retrieves a named thread channel. Added since 0.9.0
love.thread.getThread Look for a thread and get its object. Added since 0.7.0 Removed in 0.9.0
love.thread.getThreads Get all threads. Added since 0.7.0 Removed in 0.9.0
love.thread.newChannel Creates a new unnamed thread channel. Added since 0.9.0
love.thread.newThread Creates a new Thread from a filename, string or FileData object containing Lua code. Added since 0.7.0


Example

Removed in LÖVE 0.9.0
This example is not supported in that and later versions.

from http://love2d.org/forums/viewtopic.php?f=5&t=9934&p=60592&hilit=thread#p60592

main.lua

-- new thread
thread = love.thread.newThread('thread','thread.lua')
thread:start()

function love.update(dt)
   msg_table      = msg_table or {}
   -- process messages when other thread is "done"
   if thread:get('done') then
	  msg_table[1]   = thread:get('number')
	  msg_table[2]   = thread:get('string')
	  msg_table[3]   = tostring(thread:get('condition'))
	  -- concat our messages into a long string
	  message         = table.concat(msg_table,'\n')
	  -- get image data from the other thread
	  imageData      = imageData or thread:get('image')

	  -- initialize image
	  image   = love.graphics.newImage(imageData)
   end
   if thread:get('hasCounter') then
	  msg_table[4]   = thread:get('counter')
	  message         = table.concat(msg_table,'\n')
   end
end

function love.draw()
   if image then
	  love.graphics.draw(image,50,50)
   end
   if message then
	  love.graphics.print(message,10,10)
   end
end

thread.lua

-- load modules b/c new thread
require 'love.filesystem'
require 'love.image'
require 'love.timer'

-- get this thread's id
thisThread   = love.thread.getThread()
-- load image file, userdata can be sent also!
file      = love.image.newImageData('love.png')

-- send messages to this thread's inbox
thisThread:set('image',file)
thisThread:set('number',1337)
thisThread:set('string','a string message')
thisThread:set('condition',true)
thisThread:set('done',true)

time0 = love.timer.getTime()
counter = 0

-- count per second and send counter to inbox
while true do
   timeD = love.timer.getTime() - time0
   if timeD > 1 then
	  counter = counter + 1
	  thisThread:set('counter',counter)
	  thisThread:set('hasCounter',true)
	  time0 = love.timer.getTime()
   end
end

See Also

Other Languages