aboutsummaryrefslogtreecommitdiff
path: root/llthreads2/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'llthreads2/README.md')
-rw-r--r--llthreads2/README.md113
1 files changed, 113 insertions, 0 deletions
diff --git a/llthreads2/README.md b/llthreads2/README.md
new file mode 100644
index 0000000..22f2025
--- /dev/null
+++ b/llthreads2/README.md
@@ -0,0 +1,113 @@
1lua-llthreads2
2==============
3[![Build Status](https://travis-ci.org/moteus/lua-llthreads2.png?branch=master)](https://travis-ci.org/moteus/lua-llthreads2)
4[![Build Status](https://buildhive.cloudbees.com/job/moteus/job/lua-llthreads2/badge/icon)](https://buildhive.cloudbees.com/job/moteus/job/lua-llthreads2/)
5[![Build Status](https://moteus.ci.cloudbees.com/job/lua-llthreads2/badge/icon)](https://moteus.ci.cloudbees.com/job/lua-llthreads2/)
6
7This is full dropin replacement for [llthreads](https://github.com/Neopallium/lua-llthreads) library.
8
9##Incompatibility list with origin llthreads library
10* does not support Lua 5.0
11* does not support ffi interface (use Lua C API for LuaJIT)
12* returns nil instead of false on error
13* start method returns self instead of true on success
14
15##Additional
16* thread:join() method support zero timeout to check if thread alive (does not work on Windows with pthreads)
17* thread:join() method support arbitrary timeout on Windows threads
18* thread:alive() method return whether the thread is alive (does not work on Windows with pthreads)
19* set_logger function allow logging errors (crash Lua VM) in current llthread's threads
20* thread:start() has additional parameter which control in which thread child Lua VM will be destroyed
21* allow pass cfunctions to child thread (e.g. to initialize Lua state)
22
23##Usage
24
25### Use custom logger
26In this example I use [lua-log](https://github.com/moteus/lua-log) library.
27``` Lua
28-- This is child thread.
29local llthreads = require "llthreads"
30-- Send logs using ZMQ
31local LOG = require"log".new(
32 require "log.writer.net.zmq".new("tcp://127.0.0.1:5555")
33)
34llthread.set_logger(function(msg) LOG.error(msg) end)
35-- This error with traceback will be passed to logger
36error("SOME ERROR")
37```
38
39### Start attached thread collectd in child thread
40``` Lua
41-- This is main thread.
42local thread = require "llthreads".new[[
43 require "utils".sleep(5)
44]]
45
46-- We tell that we start attached thread but child Lua State shuld be close in child thread.
47-- If `thread` became garbage in main thread then finallizer calls thread:join()
48-- and main thread may hungup.
49thread:start(false, false)
50
51-- We can call join.
52-- Because of Lua state destroys in child thread we can not get
53-- returned Lua vaules so we just returns `true`.
54thread:join()
55```
56
57### Start detached joinable thread
58``` Lua
59-- This is main thread.
60local thread = require "llthreads".new[[
61 require "utils".sleep(5)
62]]
63
64-- We tell that we start detached joinable thread. In fact we start attached
65-- thread but if `thread` became garbage in main thread then finallizer just
66-- detach child thread and main thread may not hungup.
67thread:start(true, true)
68
69-- We can call join.
70-- Because of Lua state destroys in child thread we can not get
71-- returned Lua vaules so we just returns `true`.
72thread:join()
73```
74
75### Pass to child thread host application`s library loader
76If you close parent Lua state then some dynamic library may be unloaded
77and cfunction in child Lua state (thread) became invalid.
78
79``` Lua
80-- `myhost.XXX` modules is built-in modules in host application
81-- host application registers cfunction as module loader
82local preload = {}
83preload[ 'myhost.logger' ] = package.preload[ 'myhost.logger' ]
84preload[ 'myhost.config' ] = package.preload[ 'myhost.config' ]
85llthreads.new([[
86 -- registers preload
87 local preload = ...
88 for name, fn in pairs(preload) do package.preload[name] = fn end
89
90 local log = require 'myhost.logger'
91
92]], preload):start(true)
93```
94
95### Wait while thread is alive
96``` Lua
97local thread = require "llthreads".new[[
98 require "utils".sleep(5)
99 return 1
100]]
101thread:start()
102
103-- we can not use `thread:join(0)` because we can not call it twice
104-- so all returned vaules will be lost
105while thread:alive() do
106 -- do some work
107end
108
109local ok, ret = thread:join() -- true, 1
110```
111
112[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
113