Difference between revisions of "Thread"

From GiderosMobile
(removed language stuff)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:Thread -->
 
<!-- GIDEROSOBJ:Thread -->
'''<translate>Supported platforms</translate>:''' [[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/>
+
'''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/>
'''<translate>Available since</translate>:''' 2018.10<br/>
+
'''Available since:''' 2018.10<br/>
  
=== <translate>Description</translate> ===
+
=== Description ===
<translate>The Threads plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.</translate>
+
The ''Threads'' plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.
  
=== <translate>Example</translate> ===
+
=== Example ===
'''Simple example of getting result from thread.'''
+
'''Simple example of getting result from thread'''
 
<source lang="lua">require "Threads"
 
<source 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
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
ok = thread:getResult()
 
end
 
 
print("woot!")
 
print("woot!")
 
</source>
 
</source>
Line 29: Line 24:
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
+
=== Methods ===
=== <translate>Methods</translate> ===
+
[[Thread.new]] ''creates a new thread object''<br/><!--GIDEROSMTD:Thread.new() creates a new thread object-->
[[Special:MyLanguage/Thread.new|Thread.new]] ''<translate>Creates a new thread object.</translate>''<br/>
+
[[Thread:setFunction]] ''sets Lua function that thread will execute''<br/><!--GIDEROSMTD:Thread:setFunction(func) sets Lua function that thread will execute-->
<!-- GIDEROSMTD:Thread.new() Creates a new thread object. -->
+
[[Thread:execute]] ''starts/executes our thread''<br/><!--GIDEROSMTD:Thread:execute(vararg) starts/executes our thread-->
[[Special:MyLanguage/Thread:setFunction|Thread:setFunction]] ''<translate>Sets Lua function that thread will execute.</translate>''<br/>
+
[[Thread:resume]] ''resumes a thread that has been yielded''<br/><!--GIDEROSMTD:Thread:resume(...) resumes a thread that has been yielded-->
<!-- GIDEROSMTD:Thread:setFunction(func) Sets Lua function that thread will execute. -->
+
[[Thread.yield]] ''pauses executions of thread (only valid inside thread function)''<br/><!--GIDEROSMTD:Thread.yield(...) pauses executions of thread (only valid inside thread function)-->
[[Special:MyLanguage/Thread:execute|Thread:execute]] ''<translate>Starts/executes our thread</translate>''<br/>
+
[[Thread:status]] ''returns current status of the thread''<br/><!--GIDEROSMTD:Thread:status() returns current status of the thread-->
<!-- GIDEROSMTD:Thread:execute(vararg) Starts/executes our thread -->
+
[[Thread:getNumLogicalCores]] ''returns number of logical cores on host machine''<br/><!--GIDEROSMTD:Thread:getNumLogicalCores() returns number of logical cores on host machine-->
[[Special:MyLanguage/Thread:resume|Thread:resume]] ''<translate>Resumes a thread that has been yielded.</translate>''<br/>
+
[[Thread:requestTermination]] ''sets a flag to terminate thread''<br/><!--GIDEROSMTD:Thread:requestTermination() sets a flag to terminate thread-->
<!-- GIDEROSMTD:Thread:resume(...) Resumes a thread that has been yielded. -->
+
[[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)-->
[[Special:MyLanguage/Thread.yield|Thread.yield]] ''<translate>Pauses executions of thread (only a valid call inside thread function).</translate>''<br/>
+
[[Thread:fetchData]] ''returns any data sent by thread sendData call''<br/><!--GIDEROSMTD:Thread:fetchData() returns any data sent by thread sendData call-->
<!-- GIDEROSMTD:Thread.yield(...) Pauses executions of thread (only a valid call inside thread function). -->
+
[[Thread:getResults]] ''gets any results from thread''<br/><!--GIDEROSMTD:Thread:getResults(wait_time) gets any results from thread-->
[[Special:MyLanguage/Thread:status|Thread:status]] ''<translate>Returns current status of the thread.</translate>''<br/>
 
<!-- GIDEROSMTD:Thread:status() Returns current status of the thread. -->
 
[[Special:MyLanguage/Thread:getNumLogicalCores|Thread:getNumLogicalCores]] ''<translate>Returns number of logical cores on host machine.</translate>''<br/>
 
<!-- GIDEROSMTD:Thread:getNumLogicalCores() Returns number of logical cores on host machine. -->
 
[[Special:MyLanguage/Thread:requestTermination|Thread:requestTermination]] ''<translate>Sets a flag to terminate thread.</translate>''<br/>
 
<!-- GIDEROSMTD:Thread:requestTermination() Sets a flag to terminate thread. -->
 
[[Special:MyLanguage/Thread.sendData|Thread.sendData]] ''<translate>Sends data for retrieval by main thread (only a valid call inside thread function).</translate>''<br/>
 
<!-- GIDEROSMTD:Thread.sendData(...) Sends data for retrieval by main thread (only a valid call inside thread function). -->
 
[[Special:MyLanguage/Thread:fetchData|Thread:fetchData]] ''<translate>Returns any data sent by thread sendData call.</translate>''<br/>
 
<!-- GIDEROSMTD:Thread:fetchData() Returns any data sent by thread sendData call. -->
 
[[Special:MyLanguage/Thread:getResults|Thread:getResults]] ''<translate>Get any results from thread.</translate>''<br/>
 
<!-- GIDEROSMTD:Thread:getResults(wait_time) Get any results from thread. -->
 
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
=== <translate>Constants</translate> ===
+
=== Constants ===
  
 
|}
 
|}
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Revision as of 21:05, 15 April 2021

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.

Example

Simple example of getting result from 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("woot!")

Methods

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

Events

Constants