Skip to content
/ lua-chan Public
forked from easeway/lua-chan

A simple and fast message queue implementation for isolated Lua states running in multiple threads

License

Notifications You must be signed in to change notification settings

lqefn/lua-chan

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

"lua-chan" is a simple and fast message queue implementation for isolated Lua states running in multiple threads. It is quite similar to "go chan".

The philosophy that writing a multi-threading Lua program is to isolate the threads with separate Lua states. There is no shared data across the Lua states in threads. All communication and data exchange can be performed with message queues. -- This is actually the same philosophy on multi-threading as Dart language.

Usage

"lua-chan" helps you to create message queues that can be accessed by all the Lua states in the same process. A message queue is identified uniquely by a name which can be any string.

First, we need to create a "chan":

local chan = require "chan"
local ch = chan.new("mychan")

Then, we can use this "chan" to send/recv messages. In other Lua state, we can use the name to get the chan:

local chan = require "chan"
local ch = chan.get("mychan")

To use chan.get, the chan must be created using chan.new, otherwise, nil is returned.

Next, we can send/recv messages:

ch:send("message")

or

local msg = ch:recv()

"lua-chan" only implements message queues. For Lua states, lua-rings can be used, for multi-threading, lua-llthreads, lua-lanes, etc. are good options.

References

Create a chan:

chan.new(name, limit = 0)
  • name: the unique name in string to identify the chan
  • limit: the maximum messages can be queued in the chan, default is 0 which means if there is no receiver, the sender will wait; specifying -1 here means there's no limitation on the number of messages queued in the chan.

Get a chan:

chan.get(name)
  • name: the unique name of the chan to be retrieved
  • return: the retrieved chan or nil if not found

Send a message:

ch:send(message, timeout = -1)
  • message: the message to push to queue; right now the message can only be boolean, number or string.
  • timeout: the timeout in milliseconds that the sender should wait if the chan has no more capacity for queueing a message. -1 means waiting infinitely.
  • return: true if queued successfully, or false if timeout.

Receive a message:

ch:recv(timeout = -1)
  • timeout: the timeout in milliseconds that the receiver should wait until message arrives, and specifying -1 means waiting infinitely.
  • return: message or nil if timeout.

Dependencies

Only developed and tested for Lua 5.1.x on Linux. No other dependencies.

About

A simple and fast message queue implementation for isolated Lua states running in multiple threads

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 85.4%
  • Lua 14.6%