Git Product home page Git Product logo

lua-chan's Introduction

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.

lua-chan's People

Contributors

easeway avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.