gpt4 book ai didi

c - 线程上的 Lua setfenv 似乎不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:14 24 4
gpt4 key购买 nike

我想在 lua 状态加载一些函数,然后能够从 lua 线程调用这些函数。我试图在线程上 setfenv ,以便它们创建的变量仅限于线程并且不会出现在全局环境中。

lua_State *L = luaL_newstate();
luaL_openlibs(L);

dostring(L, "function f1() my_var = 100 print('var set') end");/* create func on state */
/* ^-- a wrapper which does loadstring + pcall with error handling */

lua_State *l1 = lua_newthread(L);

lua_pushthread(l1); /* l1: t */
lua_newtable(l1); /* l1: t T1{} */
lua_newtable(l1); /* l1: t T1{} T2{} */
lua_getglobal(l1, "_G"); /* l1: t T1{} T2{} _G */
lua_setfield(l1, -2, "__index"); /* l1: t T1{} T2{} ( T2.__index = _G) */
lua_setmetatable(l1, -2); /* l1: t T1 ( T1{}.mt = T2 ) */
if (!lua_setfenv(l1, -2)) /* l1: t (t.fenv = T1) */
printf("setfenv fail!\n");
lua_pop(l1, 1);

dostring(l1, "print('l1: ', my_var)"); /* --> nil (expected) */
dostring(l1, "f1() print('l1: ', my_var)"); /* --> l1: 100 (ok) */
dostring(L, "print('L: ', my_var)"); /* --> L: 100 (No!) */

我是不是做错了什么?(我不想在线程上加载函数,因为它们可能有很多,而且将它们加载一次似乎是正确的方法)

--编辑--

解决方案,似乎是:

  • 为每个线程创建一个新的环境表(__index = _G)
  • 对于其中运行的每个函数,执行 setfenv(f1, getfenv(0))

最佳答案

每个函数都有自己的 fenv。 f1的fenv是_G,所以调用的时候(不管是在哪个线程调用的),都会在_G中设置全局变量。一种选择是从 f1 显式引用线程环境,例如

function f1()
local env = getfenv(0)
env.my_var = 100
print('var set')
end

另一种方法是为每个线程提供一份 f1 的私有(private)副本。

第三种选择是使用委托(delegate)给当前线程环境(即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

关于c - 线程上的 Lua setfenv 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17563373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com