descriptionMirror of 'lua-llthreads2'
ownerAlexey Melnichuk
last changeWed, 5 Sep 2018 04:59:44 +0000 (07:59 +0300)
readme
shortlog
2018-09-05 Alexey MelnichukMerge pull request #17 from osch/master master
2018-09-04 oschavoid NULL as parameter to lua_concat in llthread_log 17/head
2018-09-04 oschavoid NULL as parameter to fputs()
2018-08-27 Alexey MelnichukMerge pull request #16 from osch/master
2018-08-27 oschinterrupted error message now contains llthreads module... 16/head
2018-08-22 oschinvoke test_interrupt.lua also in .travis.yml
2018-08-22 oschtest_interrupt.lua
2018-08-21 oschnew method thread:interrupt()
2018-07-01 Alexey MelnichukRelease 0.1.5 v0.1.5
2017-03-23 Alexey MelnichukUpdate README.md
2017-03-21 Alexey MelnichukFix. Correctly handle error from `_beginthreadex` ...
2017-02-02 Alexey MelnichukUpdate readme [ci skip]
2017-02-02 Alexey MelnichukMerge branch 'master' of https://github.com/moteus...
2017-02-02 Alexey MelnichukFix. Warning about undefined `pthread_kill` on some...
2016-09-10 Alexey MelnichukMerge pull request #7 from tst2005/patch-1
2016-09-09 TsTtypo fix 7/head
...
tags
14 months ago v0.1.5 Release 0.1.5 * Fix. Correctly...
3 years ago v0.1.4 Release 0.1.4 * Fix. Pass integer...
4 years ago v0.1.3 Release 0.1.3 * Support Lua 5.3
5 years ago v0.1.2 Release 0.1.2 Add. `llthreads.ex...
5 years ago v0.1.1 Release 0.1.1 Add. `thread:alive...
5 years ago v0.1.0 Release 0.1.0 * drop-in replacement...
heads
12 months ago master
5 years ago dist-llthreads2
5 years ago dist-llthreads2-compat
 
top

lua-llthreads2

Licence Build Status Build status

This is full dropin replacement for llthreads library.

Incompatibility list with origin llthreads library

Additional

Thread start arguments

| detached | joinable | join returns | child state closes by | gc calls | detach on | |:----------:|:----------:|:------------:|:---------------------:|:--------:|:---------:| | false | false | true | child | join | <NEVER> | |   false(*)| true(*)  | Lua values   |         parent       | join * | <NEVER> | |   true     | false(*) | raise error |         child         | <NONE> |   start   | | true | true | true | child | detach | gc |

Usage

Use custom logger

In this example I use lua-log library. Lua -- 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

``` Lua -- 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

``` Lua -- 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 loader

If you close parent Lua state then some dynamic library may be unloaded and cfunction in child Lua state (thread) became invalid.

`` Lua --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 alive

``` Lua local 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 ex module

``` Lua local Threads = require "llthreads2.ex"

local ok, v = Threads.new(function() return 1 end):start():join() assert(v == 1)

local thread = Threads.new({ -- this thread code gets changed arguments function(a, b) assert(1 == a) assert(2 == b) print("Done") end;

-- prelude can change thread arguments prelude = function(a, b) assert("1" == a) assert(nil == b) return tonumber(a), 2 end; }, "1")

thread:start():join() ```

Bitdeli Badge

top