Difference between revisions of "Thread"
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | |||
<!-- GIDEROSOBJ:Thread --> | <!-- GIDEROSOBJ:Thread --> | ||
− | ''' | + | '''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform pc.png]][[File:Platform mac.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/> |
− | ''' | + | '''Available since:''' 2018.10<br/> |
− | === | + | === 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: |
− | '''Simple example of getting result from thread | + | <syntaxhighlight lang="lua"> |
− | < | + | require "Threads" |
+ | </syntaxhighlight> | ||
+ | |||
+ | === Example === | ||
+ | '''Simple example of getting result from a thread''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | require "Threads" | ||
local thread = Thread.new() | local thread = Thread.new() | ||
thread:setFunction(function() | thread:setFunction(function() | ||
local a = 0 | local a = 0 | ||
− | for i = 1, 10000000 do | + | for i = 1, 10000000 do a += 1 end |
− | |||
− | |||
return i | return i | ||
end) | end) | ||
thread:execute() | thread:execute() | ||
local ok = false | local ok = false | ||
− | while not ok do | + | while not ok do ok = thread:getResult() end |
− | + | print("end") | |
− | end | + | </syntaxhighlight> |
− | print(" | ||
− | </ | ||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
+ | === Methods === | ||
+ | [[Thread.new]] ''creates a new thread object''<br/><!--GIDEROSMTD:Thread.new() creates a new thread object--> | ||
− | + | [[Thread:execute]] ''starts/executes the thread''<br/><!--GIDEROSMTD:Thread:execute(vararg) starts/executes the thread--> | |
− | + | [[Thread:fetchData]] ''returns any data sent by thread sendData call''<br/><!--GIDEROSMTD:Thread:fetchData() returns any data sent by thread sendData call--> | |
− | + | [[Thread:getNumLogicalCores]] ''returns number of logical cores on host machine''<br/><!--GIDEROSMTD:Thread:getNumLogicalCores() returns number of logical cores on host machine--> | |
− | + | [[Thread:getResults]] ''gets any results from thread''<br/><!--GIDEROSMTD:Thread:getResults(wait_time) gets any results from thread--> | |
− | + | [[Thread:requestTermination]] ''sets a flag to terminate thread''<br/><!--GIDEROSMTD:Thread:requestTermination() sets a flag to terminate thread--> | |
− | [[ | + | [[Thread:resume]] ''resumes a thread that has been yielded''<br/><!--GIDEROSMTD:Thread:resume(...) resumes a thread that has been yielded--> |
− | <!-- GIDEROSMTD:Thread:execute(vararg) | + | [[Thread.sendData]] ''sends data for retrieval by main thread (only valid inside thread function)''<br/><!--GIDEROSMTD:Thread.sendData(...) sends data for retrieval by main thread (only valid inside thread function)--> |
− | [[ | + | [[Thread:setFunction]] ''sets Lua function that thread will execute''<br/><!--GIDEROSMTD:Thread:setFunction(func) sets Lua function that thread will execute--> |
− | <!-- GIDEROSMTD:Thread: | + | [[Thread:status]] ''returns current status of the thread''<br/><!--GIDEROSMTD:Thread:status() returns current status of the thread--> |
− | [[ | + | [[Thread.yield]] ''pauses executions of thread (only valid inside thread function)''<br/><!--GIDEROSMTD:Thread.yield(...) pauses executions of thread (only valid inside thread function)--> |
− | <!-- GIDEROSMTD:Thread | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread: | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread: | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread: | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread.sendData(...) | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread: | ||
− | [[ | ||
− | <!-- GIDEROSMTD:Thread | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
− | === | + | === Events === |
− | === | + | === Constants === |
|} | |} | ||
{{GIDEROS IMPORTANT LINKS}} | {{GIDEROS IMPORTANT LINKS}} |
Latest revision as of 14:33, 13 July 2023
Supported platforms:
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")
MethodsThread.new creates a new thread object Thread:execute starts/executes the thread |
EventsConstants |