Thread locals in Lua(在 Lua 中线程本地人)
问题描述
在我的应用程序中,Lua 脚本可以从客户端订阅某些事件.也可以让一个脚本订阅多个客户端.目前,每次我想调用脚本时,我都会设置全局客户端",以便脚本可以访问进行回调的客户端.我想要的是类似于 C++ 中的本地线程,以便我可以为每个客户端创建一个新的 Lua 线程,并且只需为该线程设置一次客户端"变量.如果客户端随后触发该事件,它将只使用与其关联的线程.
In my application a Lua script can subscribe to certain events from a Client. It is also possible to have one script subscribe to multiple Clients. Currently I am setting the global "Client" every time I want to call the script so that the script can access the Client that made the callback. What I would want is something like a thread local in C++ so that I could create a new Lua thread for every client and just set the "Client" variable for that thread once. If the client then fires the event it would just use the thread that it is associated with.
TLDR:是否可以在 Lua 中拥有仅在特定 Lua 线程内有效的变量?
TLDR: Is it possible to have variables in Lua that are only valid within a specific Lua thread?
推荐答案
Lua 没有内置线程局部变量,但是你可以为每个 Lua 线程使用一个单独的表来存储线程局部变量,并找出哪个线程正在使用 coroutine.running
(或 C 中的 lua_pushthread
)运行.然后使用元表使其更方便.类似的东西:
Lua doesn't have thread local variables built-in, but you could use a separate table for each Lua thread to store thread local variables, and figure out which thread is running using coroutine.running
(or lua_pushthread
in C). Then make it more convenient with metatables. Something like:
local _G, coroutine = _G, coroutine
local main_thread = coroutine.running() or {} -- returns nil in Lua 5.1
local thread_locals = setmetatable( { [main_thread]=_G }, { __mode="k" } )
local TL_meta = {}
function TL_meta:__index( k )
local th = coroutine.running() or main_thread
local t = thread_locals[ th ]
if t then
return t[ k ]
else
return _G[ k ]
end
end
function TL_meta:__newindex( k, v )
local th = coroutine.running() or main_thread
local t = thread_locals[ th ]
if not t then
t = setmetatable( { _G = _G }, { __index = _G } )
thread_locals[ th ] = t
end
t[ k ] = v
end
-- convenient access to thread local variables via the `TL` table:
TL = setmetatable( {}, TL_meta )
-- or make `TL` the default for globals lookup ...
if setfenv then
setfenv( 1, TL ) -- Lua 5.1
else
_ENV = TL -- Lua 5.2+
end
这篇关于在 Lua 中线程本地人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Lua 中线程本地人
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01