Each function has its own fenv. f1
's fenv is _G
, so when called (no matter which thread it is called in), it sets the global variable in _G
.
One option is to explicitly reference the thread environment from f1
e.g.
function f1()
local env = getfenv(0)
env.my_var = 100
print('var set')
end
Another is to give each thread a private copy of f1
.
A third option is to create a proxy fenv (the same one for all threads & functions) with __index
and __newindex
metamethods that delegate to the current thread environment (i.e. getfenv(0)
.):
-- Step 1: Create the shared proxy object that delegates to the
-- current thread environment.
local tlproxy = {} -- Always empty
local tlproxy_mt = {}
function tlproxy_mt:__index(k)
return getfenv(0)[k]
end
function tlproxy_mt:__newindex(k, v)
getfenv(0)[k] = v
end
setmetatable(tlproxy, tlproxy_mt)
-- Step 2: Give each new thread a new, empty environment table.
local tenv_mt = {}
tenv_mt.__index = _G -- allows access to _G.math etc.
local function createThread(f)
local thread = coroutine.create(f)
-- These functions will not work as expected if called from the new thread,
-- so disable them.
local tenv = {
load=false, loadfile=false, loadstring=false,
module=false, require=false
}
setmetatable(tenv, tenv_mt)
debug.setfenv(thread, tenv)
return thread
end
-- Step 3: When a function should use thread-local variables, it should be
-- given 'tlproxy' as its fenv.
function f1()
my_var = 0
while true do
my_var = my_var + 1
coroutine.yield(my_var)
end
end
setfenv(f1, tlproxy)
local c1 = createThread(f1)
local c2 = createThread(f1)
-- Output should be 1, 1, 2, 2...
-- Without thread-locals it would be 1, 2, 3, 4...
for _ = 1, 100 do
print(coroutine.resume(c1))
print(coroutine.resume(c2))
end
52,1 Bot