not really much to say because the title is self-explanatory. had troubles tryna understanding them, can't find much in the wiki.
Coroutines are a very powerful feature that allow two executions to "work together".
Two coroutines use coroutine.resume
and coroutine.yield
to communicate with each other and "hand off" execution.
The simplest way for two coroutines to "work together" is to completely ignore each other and just take turns doing work. This is how ROBLOX gets the appearance of "multitasking": wait()
causes the current coroutine to stop working and lets another coroutine start working.
If you're only using coroutine
to make two threads work "at the same time", don't use coroutine.
Instead, use spawn
to create a "thread" in the background.
See this thread for examples of (not) using spawn to great effect.
Consider the following function that produces prime numbers:
-- a prime number is any number larger than 1 not divisible -- any smaller (prime) number function computePrimes() local primes = {} while #primes < 1000 do local isPrime = true for _, smallerPrime in ipairs(primes) do -- if `i` is divisible by `smallerPrime` if i % smallerPrime == 0 then isPrime = false end end if isPrime then table.insert(primes, i) end end return primes end
The above function produces a list; you can use a simple loop to print out the first 1000 primes:
for _, prime in ipairs(computePrimes()) do print(prime) end
Or you could do something like, compute the sum of all of the primes under 10000:
local sum = 0 for _, prime in ipairs(computePrimes()) do if prime >= 10000 then break end sum = sum + prime end print(sum)
There are two main problems with this approach.
1) computePrimes()
might not have made enough primes; it's possible it stopped too early
2) computePrimes()
might have wasted time making way more primes than we need; it's possible the loop doesn't even cover most of the list.
We could plumb this information into computePrimes
, but that would make computePrimes
much more complicated.
The solution is to use coroutines; then computePrimes
will be a generator that produces 1 prime at a time:
primeComputer = coroutine.wrap(function() local primes = {} for i = 2, math.huge do local isPrime = true for _, smallerPrime in ipairs(primes) do -- if `i` is divisible by `smallerPrime` if i % smallerPrime == 0 then isPrime = false end end if isPrime then table.insert(primes, i) -- `prime` is the value that this generator -- will return each time it is called coroutine.yield(i) end end end)
I use coroutine.wrap
as a convenience, I won't give an example here that uses coroutine.resume
/ coroutine.create
manually.
Now, we can write our loop and compute exactly the set of primes we want:
local sum = 0 while true do -- ask for one more prime local prime = primeComputer() if prime >= 10000 then break end sum = sum + prime end print(sum)