Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

difference of spawn() and coroutine?

Asked by
noposts 75
7 years ago

not really much to say because the title is self-explanatory. had troubles tryna understanding them, can't find much in the wiki.

0
Imo, there isn't really much of a difference; however, in terms of use, I believe using `coroutine` has more of an advantage for /some/ things. TheeDeathCaster 2368 — 7y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

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)
0
hm thanks for making me understand. also appreciate you typing those up noposts 75 — 7y
Ad

Answer this question