Module lzmq.threads

Create threads and pass Context to it.

Functions

run (ctx, code, ...) Create new thread.
fork (ctx, code, ...) Create new thread and pipe.
actor (ctx, code, ...) Create new arctor.
xrun (code, ...) Create new thread.
xfork (code, ...) Create new thread and pipe.
xactor (code, ...) Create new arctor.
context () Get context from parent thread.
set_context (ctx) Set context as global for thread library.
get_parent_ctx () Get context from parent thread.

Class thread

thread:start ([detached=true[, joinable=true]]) Resume thread.
thread:join () Join to joinable thread.
thread:alive () Check if thread is working.


Functions

run (ctx, code, ...)
Create new thread. This function create thread in suspended mode.

Parameters:

  • ctx lzmq.context ZMQ Context
  • code [string or function] if first char is '@' then this is filename else this is Lua chunk. If code is function then it uses `string.dump` to serialize it.
  • ... arguments for thread

Returns:

    thread new thread

Or

  1. nil
  2. string error message

Usage:

     local thread = zthreads.run(ctx, function()
       local ctx = require"lzmq.threads".context()
       ...
     end):start(true)
fork (ctx, code, ...)
Create new thread and pipe. This function create thread in suspended mode.

Parameters:

  • ctx lzmq.context ZMQ Context
  • code [string or function] if first char is '@' then this is filename else this is Lua chunk. If code is function then it uses `string.dump` to serialize it.
  • ... arguments for thread

Returns:

  1. thread new thread
  2. lzmq.socket pipe

Or

  1. nil
  2. lzmq.error error object

Or

  1. nil
  2. string error message

Usage:

     local thread, pipe = zthreads.fork(ctx, function(pipe)
       pipe:send("READY")
       pipe:recv()
     end)
     thread:start()
    
     pipe:recv()
     pipe:send("START")
actor (ctx, code, ...)
Create new arctor. This function create thread in suspended mode. `arctor` is object inherited from thread and zmq socket. You can use it to communicate with thread (send/sendx/recv/recvx) and to sync with thread (join).
You can also usage actor with zmq poller and zmq loop objects.
Child thread gets pipe socket same as in fork.

Parameters:

  • ctx lzmq.context ZMQ Context
  • code [string or function] if first char is '@' then this is filename else this is Lua chunk. If code is function then it uses `string.dump` to serialize it.
  • ... arguments for thread

Returns:

    arctor new actor

Or

  1. nil
  2. lzmq.error error object

Or

  1. nil
  2. string error message

Usage:

     local actor = zthreads.actor(ctx, function(pipe)
       pipe:send("READY")
       pipe:recv()
     end):start()
    
     actor:recv()
     actor:send("START")
     actor:join()
xrun (code, ...)
Create new thread. This function is fully analog of `fork` function but always use globla context.

Parameters:

  • code [string or function]
  • ... arguments

See also:

Usage:

     local thread = zthreads.xrun(function()
       local ctx = require"lzmq.threads".context()
       ...
     end):start(true)
xfork (code, ...)
Create new thread and pipe. This function is fully analog of `fork` function but always use globla context.

Parameters:

  • code [string or function]
  • ... arguments

See also:

Usage:

     local thread, pipe = zthreads.xfork(function(pipe)
       pipe:send("READY")
       pipe:recv()
     end)
     thread:start()
    
     pipe:recv()
     pipe:send("START")
xactor (code, ...)
Create new arctor. This function is fully analog of `actor` function but always use globla context.

Parameters:

  • code [string or function]
  • ... arguments

See also:

Usage:

     local actor = zthreads.xactor(function(pipe)
       pipe:send("READY")
       pipe:recv()
     end):start()
    
     actor:recv()
     actor:send("START")
     actor:join()
context ()
Get context from parent thread. For main thread this function can create new context and set it as parent context. So you can use this function to get unique global zmq context. If you lready have context in main thread then you can use `set_context` function

Returns:

    lzmq.context
set_context (ctx)
Set context as global for thread library. This function raise error in case if context already set.

Parameters:

get_parent_ctx ()
Get context from parent thread. This function is alias for `context` function

Returns:

    lzmq.context

Class thread

thread:start ([detached=true[, joinable=true]])
Resume thread.

Warning: If you don't call thread:join() on a joinable child thread, it will be called by the garbage collector, which may cause random pauses/freeze of the parent thread.

Parameters:

  • detached bool start detached thread (default true)
  • joinable bool start joinable thread (default true)

Returns:

    true
thread:join ()
Join to joinable thread.

Returns:

    ... return values from thread
thread:alive ()
Check if thread is working.

Returns:

    bool return true if thread is alive
generated by LDoc 1.4.0