在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):moteus/lua-llthreads2开源软件地址(OpenSource Url):https://github.com/moteus/lua-llthreads2开源编程语言(OpenSource Language):Lua 44.9%开源软件介绍(OpenSource Introduction):lua-llthreads2This is full dropin replacement for llthreads library. Incompatibility list with origin llthreads library
Additional
Thread start arguments
UsageUse custom loggerIn this example I use lua-log library. -- This is child thread.
local llthreads = require "llthreads2"
-- Send logs using ZMQ
local LOG = require"log".new(
require "log.writer.net.zmq".new("tcp://127.0.0.1:5555")
)
llthread.set_logger(function(msg) LOG.error(msg) end)
-- This error with traceback will be passed to logger
error("SOME ERROR") Start attached thread collectd in child thread-- This is main thread.
local thread = require "llthreads2".new[[
require "utils".sleep(5)
]]
-- We tell that we start attached thread but child Lua State shuld be close in child thread.
-- If `thread` became garbage in main thread then finallizer calls thread:join()
-- and main thread may hungup. But because of child state closes in child thread all objects
-- in this state can be destroyed and we can prevent deadlock.
thread:start(false, false)
-- We can call join.
-- Because of Lua state destroys in child thread we can not get
-- returned Lua values so we just returns `true`.
thread:join() Start detached joinable thread-- This is main thread.
local thread = require "llthreads2".new[[
require "utils".sleep(5)
]]
-- We tell that we start detached joinable thread. In fact we start attached
-- thread but if `thread` became garbage in main thread then finallizer just
-- detach child thread and main thread may not hungup.
thread:start(true, true)
-- We can call join.
-- Because of Lua state destroys in child thread we can not get
-- returned Lua values so we just returns `true`.
thread:join() Pass to child thread host application`s library loaderIf you close parent Lua state then some dynamic library may be unloaded and cfunction in child Lua state (thread) became invalid. -- `myhost.XXX` modules is built-in modules in host application
-- host application registers cfunction as module loader
local preload = {}
preload[ 'myhost.logger' ] = package.preload[ 'myhost.logger' ]
preload[ 'myhost.config' ] = package.preload[ 'myhost.config' ]
llthreads.new([[
-- registers preload
local preload = ...
for name, fn in pairs(preload) do package.preload[name] = fn end
local log = require 'myhost.logger'
]], preload):start(true) Wait while thread is alivelocal thread = require "llthreads2".new[[
require "utils".sleep(5)
return 1
]]
thread:start()
-- we can not use `thread:join(0)` because we can not call it twice
-- so all returned values will be lost
while thread:alive() do
-- do some work
end
local ok, ret = thread:join() -- true, 1
Use |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论