Difference between revisions of "love.thread"

m (???)
(Types)
Line 13: Line 13:
  
 
== Types ==
 
== Types ==
{{#ask: [[Category:Types]] [[parent::love.thread]] [[Concept:Current]]
+
{{#ask: [[Category:Types (Français)]] [[parent::love.thread (Français)]] [[Concept:Current]]
 
| headers=hide
 
| headers=hide
 
| format=template
 
| format=template
Line 23: Line 23:
 
| ?PrettyRemoved
 
| ?PrettyRemoved
 
}}
 
}}
 +
 
== Functions ==
 
== Functions ==
 
{{#ask: [[Category:Functions]] [[parent::love.thread]] [[Concept:Current]]
 
{{#ask: [[Category:Functions]] [[parent::love.thread]] [[Concept:Current]]

Revision as of 02:27, 28 December 2020

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 LÖVE 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 love.data, love.filesystem, and love.thread module. Every other module has to be loaded with require.

O.png The love.graphics, love.window, love.joystick, love.keyboard, love.mouse, and love.touch modules have several restrictions and therefore can only be used in the main thread.  


O.png Unless you define the love.threaderror callback or call Thread:getError you won't see any errors your thread code throws.  


O.png In Android, you have to make sure all threads are terminated before quitting/restarting, otherwise LOVE will fail to start again!  


Types

Channel (Français) Un objet pouvant être utilisé pour envoyer et recevoir de données entre les threads (fils d'exécution). Added since 0.9.0
Thread (Français) Un Thread représente un fil d'exécution (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


Examples

A simple example showing the general usage of a thread and using channels for communication.

-- This is the code that's going to run on the our thread. It should be moved
-- to its own dedicated Lua file, but for simplicity's sake we'll create it
-- here.
local threadCode = [[
-- Receive values sent via thread:start
local min, max = ...

for i = min, max do
    -- The Channel is used to handle communication between our main thread and
    -- this thread. On each iteration of the loop will push a message to it which
    -- we can then pop / receive in the main thread.
    love.thread.getChannel( 'info' ):push( i )
end
]]

local thread -- Our thread object.
local timer  -- A timer used to animate our circle.

function love.load()
    thread = love.thread.newThread( threadCode )
    thread:start( 99, 1000 )
end

function love.update( dt )
    timer = timer and timer + dt or 0

    -- Make sure no errors occured.
    local error = thread:getError()
    assert( not error, error )
end

function love.draw()
    -- Get the info channel and pop the next message from it.
    local info = love.thread.getChannel( 'info' ):pop()
    if info then
        love.graphics.print( info, 10, 10 )
    end

    -- We smoothly animate a circle to show that the thread isn't blocking our main thread.
    love.graphics.circle( 'line', 100 + math.sin( timer ) * 20, 100 + math.cos( timer ) * 20, 20 )
end

See Also

Other Languages