Difference between revisions of "Thread"

From GiderosMobile
(reorder functions by name)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
  
 
=== Description ===
 
=== Description ===
The ''Threads'' plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.
+
The '''Threads''' plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.
 +
 
 +
To add the '''Threads''' plugin to your application you call:
 +
<syntaxhighlight lang="lua">
 +
require "Threads"
 +
</syntaxhighlight>
  
 
=== Example ===
 
=== Example ===
'''Simple example of getting result from thread'''
+
'''Simple example of getting result from a thread'''
<source lang="lua">require "Threads"
+
<syntaxhighlight lang="lua">
 +
require "Threads"
 
local thread = Thread.new()
 
local thread = Thread.new()
 
thread:setFunction(function()
 
thread:setFunction(function()
Line 19: Line 25:
 
local ok = false
 
local ok = false
 
while not ok do ok = thread:getResult() end
 
while not ok do ok = thread:getResult() end
print("woot!")
+
print("end")
</source>
+
</syntaxhighlight>
  
 
{|-
 
{|-

Latest revision as of 14:33, 13 July 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform pc.pngPlatform mac.pngPlatform winrt.pngPlatform win32.png
Available since: 2018.10

Description

The Threads plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.

To add the Threads plugin to your application you call:

require "Threads"

Example

Simple example of getting result from a thread

require "Threads"
local thread = Thread.new()
thread:setFunction(function()
	local a = 0
	for i = 1, 10000000 do a += 1 end
	return i
end)
thread:execute()
local ok = false
while not ok do ok = thread:getResult() end
print("end")

Methods

Thread.new creates a new thread object

Thread:execute starts/executes the thread
Thread:fetchData returns any data sent by thread sendData call
Thread:getNumLogicalCores returns number of logical cores on host machine
Thread:getResults gets any results from thread
Thread:requestTermination sets a flag to terminate thread
Thread:resume resumes a thread that has been yielded
Thread.sendData sends data for retrieval by main thread (only valid inside thread function)
Thread:setFunction sets Lua function that thread will execute
Thread:status returns current status of the thread
Thread.yield pauses executions of thread (only valid inside thread function)

Events

Constants