Lua4z
A binary distribution of Lua for z/OS, with batteries
-- Multithreaded programming example using Lua Lanes
--
-- A simple demonstration that creates two threads,
-- a producer and a consumer, and calculates the rate (in MB/s)
-- at which data is sent through a linda message queue

local lanes = require "lanes".configure()
local timer = require "timer"

-- set the number of messages to send
local numMsgs = arg[1] or 50000

-- start the timer
local t = timer()

-- create the linda channel for communication between threads
local linda = lanes.linda()

-- the message, 1K of zeros
local MSG_SIZE = 1024
local msg = string.rep(0, MSG_SIZE)

-- the producer thread function
local function producer(max)
  for i = 1, max do
    linda:send("x", msg)  -- send the message
  end
end

-- start the producer thread function
local a = lanes.gen("", producer)(numMsgs)

-- the consumer loop receives all the message from the producer
local count = 0
while true do
  local key, val = linda:receive(0.01, "x")  -- timeout in seconds
  if val == nil then
    break -- producer has finished
  end
  count = count + 1
end

local bytes = count * MSG_SIZE
local rate = math.floor((bytes / 1000000) / t:elapsed())
print("MsgCnt: " .. count .. ", CPUTime: "..  t:elapsed() ..
      ", Rate: " .. rate .. " MB/s")