<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.cheatengine.org/index.php?action=history&amp;feed=atom&amp;title=Lua%3AcreateThread</id>
	<title>Lua:createThread - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.cheatengine.org/index.php?action=history&amp;feed=atom&amp;title=Lua%3AcreateThread"/>
	<link rel="alternate" type="text/html" href="https://wiki.cheatengine.org/index.php?title=Lua:createThread&amp;action=history"/>
	<updated>2026-06-28T00:12:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>https://wiki.cheatengine.org/index.php?title=Lua:createThread&amp;diff=8531&amp;oldid=prev</id>
		<title>Leunsel: Initial page creation.</title>
		<link rel="alternate" type="text/html" href="https://wiki.cheatengine.org/index.php?title=Lua:createThread&amp;diff=8531&amp;oldid=prev"/>
		<updated>2026-06-27T01:06:42Z</updated>

		<summary type="html">&lt;p&gt;Initial page creation.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Lua]]&lt;br /&gt;
{{CodeBox|'''function''' createThread(''function''(''thread'', ''...''), ''...'' OPTIONAL) ''':''' Thread}}&lt;br /&gt;
&lt;br /&gt;
Executes the given function in another thread using the system's thread mechanism.&lt;br /&gt;
&lt;br /&gt;
The function returns a [[Lua:Class:Thread|Thread]] class object. The thread function receives the Thread object as its first parameter, followed by any additional parameters passed to &amp;lt;code&amp;gt;createThread&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Function Declaration===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
function(thread, ...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Function Parameters===&lt;br /&gt;
{|width=&amp;quot;85%&amp;quot; cellpadding=&amp;quot;10%&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;0&amp;quot;&lt;br /&gt;
!align=&amp;quot;left&amp;quot;|Parameter&lt;br /&gt;
!align=&amp;quot;left&amp;quot;|Type&lt;br /&gt;
!style=&amp;quot;width: 80%;background-color:white;&amp;quot; align=&amp;quot;left&amp;quot;|Description&lt;br /&gt;
|-&lt;br /&gt;
|function&lt;br /&gt;
|Function&lt;br /&gt;
|The function to execute in the new thread. It receives the Thread object as its first argument.&lt;br /&gt;
|-&lt;br /&gt;
|...&lt;br /&gt;
|Any OPTIONAL&lt;br /&gt;
|Optional arguments that are passed to the thread function after the Thread object.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
[[Lua:Class:Thread|Thread]] — The created Thread object.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
&lt;br /&gt;
====Create a basic thread====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThread(function(thread)&lt;br /&gt;
  print(&amp;quot;Thread started&amp;quot;)&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use the Thread parameter====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThread(function(thread)&lt;br /&gt;
  print(&amp;quot;Thread object: &amp;quot; .. tostring(thread))&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Pass arguments to the thread function====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;4&amp;quot;&amp;gt;&lt;br /&gt;
local text = &amp;quot;Hello from another thread&amp;quot;&lt;br /&gt;
local count = 3&lt;br /&gt;
&lt;br /&gt;
local thread = createThread(function(thread, message, repetitions)&lt;br /&gt;
  for i = 1, repetitions do&lt;br /&gt;
    print(message .. &amp;quot; #&amp;quot; .. tostring(i))&lt;br /&gt;
  end&lt;br /&gt;
end, text, count)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Run repeated work in a thread====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThread(function(thread)&lt;br /&gt;
  for i = 1, 5 do&lt;br /&gt;
    print(&amp;quot;Worker step &amp;quot; .. tostring(i))&lt;br /&gt;
    sleep(500)&lt;br /&gt;
  end&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use synchronize for main-thread work====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,4&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThread(function(thread)&lt;br /&gt;
  local result = &amp;quot;Finished background work&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  synchronize(function()&lt;br /&gt;
    print(result)&lt;br /&gt;
  end)&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Create a thread from a named function====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;7&amp;quot;&amp;gt;&lt;br /&gt;
local function worker(thread, message)&lt;br /&gt;
  print(message)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local message = &amp;quot;Running named worker&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local thread = createThread(worker, message)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Pass multiple values====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThread(function(thread, a, b, c)&lt;br /&gt;
  print(&amp;quot;A: &amp;quot; .. tostring(a))&lt;br /&gt;
  print(&amp;quot;B: &amp;quot; .. tostring(b))&lt;br /&gt;
  print(&amp;quot;C: &amp;quot; .. tostring(c))&lt;br /&gt;
end, 10, &amp;quot;text&amp;quot;, true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Store the returned Thread object====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local workerThread = createThread(function(thread)&lt;br /&gt;
  print(&amp;quot;Thread is running&amp;quot;)&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Returned thread: &amp;quot; .. tostring(workerThread))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Guard thread creation behind a condition====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;4&amp;quot;&amp;gt;&lt;br /&gt;
local shouldStart = true&lt;br /&gt;
&lt;br /&gt;
if shouldStart then&lt;br /&gt;
  createThread(function(thread)&lt;br /&gt;
    print(&amp;quot;Conditional thread started&amp;quot;)&lt;br /&gt;
  end)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{LuaSeeAlso}}&lt;br /&gt;
&lt;br /&gt;
{{Threads}}&lt;/div&gt;</summary>
		<author><name>Leunsel</name></author>
		
	</entry>
</feed>