lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Tue, Nov 18, 2014 at 6:04 PM, arioch82 <arioch82@gmail.com> wrote:
if that's the case and i create and run multiple coroutines on the same
non-anonymous function how would all variables local to the function behave?
they would all resume each other and share the same data?
that would also mean that all locally created userdata/tables will never be
free since there will always be a variable in the module pointing to that
function



--
View this message in context: http://lua.2524044.n2.nabble.com/coroutines-local-userdata-and-GC-tp7664107p7664135.html
Sent from the Lua-l mailing list archive at Nabble.com.

The function is a single reference, but when it runs, its arguments and locals are executed in its own space.

The upvalues of this function are shared. And so:

```
local a = 1

my_func = function (b)
  b = tonumber(b) or 1
  local c = 1
  for i = 1, 10 do
    a = a + 1
    b = b + 1
    c = c + 1
    print('a', a, 'b', b, 'c', c)
    coroutine.yield()
  end
end


---make a bunch of coroutines with my_func as the main

---resume them. you'll see that each coroutine will cause the single `a` variable to increment
---but `b` and `c` are all independent.

```

-Andrew