Difference between revisions of "love.thread"

m
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
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.
 
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.
+
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 the love.thread module. Every other module has to be loaded with [[require]].
 
When a [[Thread]] is started, it only loads the love.thread module. Every other module has to be loaded with [[require]].
  
{{notice|The [[love.graphics]] and [[love.window]] modules have several restrictions and therefore should only be used in the main thread.}}
+
{{notice|The [[love.graphics]] and [[love.window]] modules have several restrictions and therefore can only be used in the main thread.}}
 
{{notice|Unless you define the [[love.threaderror]] callback or call [[Thread:getError]] you won't see any errors your thread code throws.}}
 
{{notice|Unless you define the [[love.threaderror]] callback or call [[Thread:getError]] you won't see any errors your thread code throws.}}
{{notice|If your get "module not found" error in your thread, include [[love.filesystem]] first. This will fix require paths. }}
 
  
 
== Types ==
 
== Types ==

Revision as of 16:41, 13 July 2017

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 the love.thread module. Every other module has to be loaded with require.

O.png The love.graphics and love.window 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.  


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


Examples

For 0.9.0: http://love2d.org/forums/viewtopic.php?f=4&t=76670

See Also

Other Languages