<legend id='aXn91'><style id='aXn91'><dir id='aXn91'><q id='aXn91'></q></dir></style></legend><tfoot id='aXn91'></tfoot>

<small id='aXn91'></small><noframes id='aXn91'>

      • <bdo id='aXn91'></bdo><ul id='aXn91'></ul>
    1. <i id='aXn91'><tr id='aXn91'><dt id='aXn91'><q id='aXn91'><span id='aXn91'><b id='aXn91'><form id='aXn91'><ins id='aXn91'></ins><ul id='aXn91'></ul><sub id='aXn91'></sub></form><legend id='aXn91'></legend><bdo id='aXn91'><pre id='aXn91'><center id='aXn91'></center></pre></bdo></b><th id='aXn91'></th></span></q></dt></tr></i><div id='aXn91'><tfoot id='aXn91'></tfoot><dl id='aXn91'><fieldset id='aXn91'></fieldset></dl></div>

        在 Lua 中线程本地人

        Thread locals in Lua(在 Lua 中线程本地人)
        <i id='jjCxN'><tr id='jjCxN'><dt id='jjCxN'><q id='jjCxN'><span id='jjCxN'><b id='jjCxN'><form id='jjCxN'><ins id='jjCxN'></ins><ul id='jjCxN'></ul><sub id='jjCxN'></sub></form><legend id='jjCxN'></legend><bdo id='jjCxN'><pre id='jjCxN'><center id='jjCxN'></center></pre></bdo></b><th id='jjCxN'></th></span></q></dt></tr></i><div id='jjCxN'><tfoot id='jjCxN'></tfoot><dl id='jjCxN'><fieldset id='jjCxN'></fieldset></dl></div>
          <bdo id='jjCxN'></bdo><ul id='jjCxN'></ul>

        • <small id='jjCxN'></small><noframes id='jjCxN'>

              <tfoot id='jjCxN'></tfoot>

                  <tbody id='jjCxN'></tbody>

                • <legend id='jjCxN'><style id='jjCxN'><dir id='jjCxN'><q id='jjCxN'></q></dir></style></legend>
                  本文介绍了在 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 中线程本地人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                  <i id='lGS20'><tr id='lGS20'><dt id='lGS20'><q id='lGS20'><span id='lGS20'><b id='lGS20'><form id='lGS20'><ins id='lGS20'></ins><ul id='lGS20'></ul><sub id='lGS20'></sub></form><legend id='lGS20'></legend><bdo id='lGS20'><pre id='lGS20'><center id='lGS20'></center></pre></bdo></b><th id='lGS20'></th></span></q></dt></tr></i><div id='lGS20'><tfoot id='lGS20'></tfoot><dl id='lGS20'><fieldset id='lGS20'></fieldset></dl></div>

                • <tfoot id='lGS20'></tfoot>

                      <tbody id='lGS20'></tbody>
                      • <bdo id='lGS20'></bdo><ul id='lGS20'></ul>

                        <small id='lGS20'></small><noframes id='lGS20'>

                          <legend id='lGS20'><style id='lGS20'><dir id='lGS20'><q id='lGS20'></q></dir></style></legend>