Difference between revisions of "Thread"

From GiderosMobile
(merged the threads plugin with the thread class)
Line 4: Line 4:
 
'''<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/>
 
'''<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/>
 
'''<translate>Available since</translate>:''' 2018.10<br/>
 
'''<translate>Available since</translate>:''' 2018.10<br/>
 +
 
=== <translate>Description</translate> ===
 
=== <translate>Description</translate> ===
<translate>Main Thread class that concurrent threads are created with and managed by. From [[Special:MyLanguage/Threads|Threads]] plugin.
+
<translate>The Threads plugin provides the ability to run Lua code in separate threads, taking advantage of multiple cores where available.</translate>
  </translate><br/>
+
 
 +
=== <translate>Example</translate> ===
 +
'''Simple example of getting result from thread.'''
 +
<source lang="lua">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!")
 +
</source>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
  
 
=== <translate>Methods</translate> ===
 
=== <translate>Methods</translate> ===
[[Special:MyLanguage/Thread.new|Thread.new]] ''<translate>Creates a new thread object.</translate>''<br/><!-- GIDEROSMTD:Thread.new() Creates a new thread object. -->
+
[[Special:MyLanguage/Thread.new|Thread.new]] ''<translate>Creates a new thread object.</translate>''<br/>
[[Special:MyLanguage/Thread:setFunction|Thread:setFunction]] ''<translate>Sets Lua function that thread will execute.</translate>''<br/><!-- GIDEROSMTD:Thread:setFunction(func) Sets Lua function that thread will execute. -->
+
<!-- GIDEROSMTD:Thread.new() Creates a new thread object. -->
[[Special:MyLanguage/Thread:execute|Thread:execute]] ''<translate>Starts/executes our thread</translate>''<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/>
[[Special:MyLanguage/Thread:resume|Thread:resume]] ''<translate>Resumes a thread that has been yielded.</translate>''<br/><!-- GIDEROSMTD:Thread:resume(...) Resumes a thread that has been yielded. -->
+
<!-- GIDEROSMTD:Thread:setFunction(func) Sets Lua function that thread will execute. -->
[[Special:MyLanguage/Thread.yield|Thread.yield]] ''<translate>Pauses executions of thread (only a valid call inside thread function).</translate>''<br/><!-- GIDEROSMTD:Thread.yield(...) Pauses executions of thread (only a valid call inside thread function). -->
+
[[Special:MyLanguage/Thread:execute|Thread:execute]] ''<translate>Starts/executes our thread</translate>''<br/>
[[Special:MyLanguage/Thread:status|Thread:status]] ''<translate>Returns current status of the thread.</translate>''<br/><!-- GIDEROSMTD:Thread:status() Returns current status of the thread. -->
+
<!-- GIDEROSMTD:Thread:execute(vararg) Starts/executes our 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:resume|Thread:resume]] ''<translate>Resumes a thread that has been yielded.</translate>''<br/>
[[Special:MyLanguage/Thread:requestTermination|Thread:requestTermination]] ''<translate>Sets a flag to terminate thread.</translate>''<br/><!-- GIDEROSMTD:Thread:requestTermination() Sets a flag to terminate thread. -->
+
<!-- GIDEROSMTD:Thread:resume(...) Resumes a thread that has been yielded. -->
[[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.yield|Thread.yield]] ''<translate>Pauses executions of thread (only a valid call inside thread function).</translate>''<br/>
[[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. -->
+
<!-- GIDEROSMTD:Thread.yield(...) Pauses executions of thread (only a valid call inside thread function). -->
[[Special:MyLanguage/Thread:getResults|Thread:getResults]] ''<translate>Get any results from thread.</translate>''<br/><!-- GIDEROSMTD:Thread:getResults(wait_time) Get 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> ===
 
=== <translate>Events</translate> ===
 
''none''<br/>
 
 
 
=== <translate>Constants</translate> ===
 
=== <translate>Constants</translate> ===
 
''none''<br/>
 
  
 
|}
 
|}

Revision as of 09:04, 5 January 2020


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 a valid call 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 a valid call inside thread function).
Thread:fetchData Returns any data sent by thread sendData call.
Thread:getResults Get any results from thread.

Events

Constants