<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.inspired-lua.org/index.php?action=history&amp;feed=atom&amp;title=coroutines</id>
	<title>coroutines - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.inspired-lua.org/index.php?action=history&amp;feed=atom&amp;title=coroutines"/>
	<link rel="alternate" type="text/html" href="https://wiki.inspired-lua.org/index.php?title=coroutines&amp;action=history"/>
	<updated>2026-04-09T00:38:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://wiki.inspired-lua.org/index.php?title=coroutines&amp;diff=1222&amp;oldid=prev</id>
		<title>Technolapin: Created page with &quot;The coroutines in lua are pieces of codes that can be execute or stoped easely.  A coroutine is declared with a function:  &lt;source lang=&quot;lua&quot;&gt; function foo (a)  print a+1 end loc...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.inspired-lua.org/index.php?title=coroutines&amp;diff=1222&amp;oldid=prev"/>
		<updated>2014-11-08T19:34:28Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;The coroutines in lua are pieces of codes that can be execute or stoped easely.  A coroutine is declared with a function:  &amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt; function foo (a)  print a+1 end loc...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The coroutines in lua are pieces of codes that can be execute or stoped easely.&lt;br /&gt;
&lt;br /&gt;
A coroutine is declared with a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function foo (a)&lt;br /&gt;
 print a+1&lt;br /&gt;
end&lt;br /&gt;
local co = coroutine.create (foo)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run a coroutine, you must use coroutine.resume() :  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function foo (a)&lt;br /&gt;
 print a+1&lt;br /&gt;
end&lt;br /&gt;
local co = coroutine.create (foo)&lt;br /&gt;
coroutine.resume (co, 2)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The syntax is : &lt;br /&gt;
coroutine.resume (coroutinetorun, arg1, arg2, arg3, ...)&lt;br /&gt;
&lt;br /&gt;
So now the code will return 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But coroutines are much more usefull with using coroutine.yield.&lt;br /&gt;
That function will stop the execution of the coroutine, that can be resumed with coroutine.resume.&lt;br /&gt;
The coroutine.resume() will return all argument in the coroutine.yield()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function foo ()&lt;br /&gt;
 local a = 0&lt;br /&gt;
 while true do&lt;br /&gt;
  a=a+1&lt;br /&gt;
  coroutine.yield (a, &amp;quot;an useless string just to show that more than one value can be returned&amp;quot;)&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
local co = coroutine.create (foo)&lt;br /&gt;
print (coroutine.resume (co))      -- return if the coroutine runed and the values in the coroutine.yield&lt;br /&gt;
&lt;br /&gt;
print (coroutine.resume (co))      -- one more time!&lt;br /&gt;
print (coroutine.resume (co))      -- again&lt;br /&gt;
print (coroutine.resume (co))      -- a last one to finish.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The status of the coroutine can be returned with coroutine.status()&lt;br /&gt;
The possible status are:&lt;br /&gt;
 - suspended   -- the coroutine is suspended and can be resumed&lt;br /&gt;
 - dead        -- the coroutine is terminated and wont do anything&lt;br /&gt;
&lt;br /&gt;
Instead of coroutine.create () you can use coroutine.wrap, that return a function wich contain the coroutine.resume() of the function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
coroutine.running () will return the currently running coroutine. That can be usefull if you use several coroutines.&lt;/div&gt;</summary>
		<author><name>Technolapin</name></author>
		
	</entry>
</feed>