{"id":13896696,"url":"https://github.com/moteus/lua-llthreads2","last_synced_at":"2025-04-15T11:50:50.454Z","repository":{"id":12774900,"uuid":"15448386","full_name":"moteus/lua-llthreads2","owner":"moteus","description":"`llthreads` library rewritten without `LuaNativeObjects` code generator","archived":false,"fork":false,"pushed_at":"2023-10-16T20:55:18.000Z","size":143,"stargazers_count":76,"open_issues_count":4,"forks_count":22,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-28T19:44:49.432Z","etag":null,"topics":["lua","lua-binding","multithreading","pthreads","threading"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moteus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2013-12-26T07:49:03.000Z","updated_at":"2024-11-06T22:05:22.000Z","dependencies_parsed_at":"2024-01-08T20:19:18.258Z","dependency_job_id":null,"html_url":"https://github.com/moteus/lua-llthreads2","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-llthreads2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-llthreads2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-llthreads2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-llthreads2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moteus","download_url":"https://codeload.github.com/moteus/lua-llthreads2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067152,"owners_count":21207392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","lua-binding","multithreading","pthreads","threading"],"created_at":"2024-08-06T18:03:05.714Z","updated_at":"2025-04-15T11:50:50.434Z","avatar_url":"https://github.com/moteus.png","language":"Lua","readme":"lua-llthreads2\n==============\n[![Licence](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENSE)\n[![Build Status](https://travis-ci.org/moteus/lua-llthreads2.png?branch=master)](https://travis-ci.org/moteus/lua-llthreads2)\n[![Build status](https://ci.appveyor.com/api/projects/status/st2uldkbqmpgfl2w?svg=true)](https://ci.appveyor.com/project/moteus/lua-llthreads2)\n\nThis is full dropin replacement for [llthreads](https://github.com/Neopallium/lua-llthreads) library.\n\n## Incompatibility list with origin llthreads library\n* does not support Lua 5.0\n* does not support ffi interface (use Lua C API for LuaJIT)\n* returns nil instead of false on error\n* start method returns self instead of true on success\n\n## Additional\n* thread:join() method support zero timeout to check if thread alive (does not work on Windows with pthreads)\n* thread:join() method support arbitrary timeout on Windows threads\n* thread:alive() method return whether the thread is alive (does not work on Windows with pthreads)\n* set_logger function allow logging errors (crash Lua VM) in current llthread's threads\n* thread:start() has additional parameter which control in which thread child Lua VM will be destroyed\n* allow pass cfunctions to child thread (e.g. to initialize Lua state)\n\n## Thread start arguments\n| `detached` | `joinable` | join returns | child state closes by | gc calls | detach on |\n|:----------:|:----------:|:------------:|:---------------------:|:--------:|:---------:|\n|   false    |  false     | `true`       |         child         |  join    | `\u003cNEVER\u003e` |\n|   false(\\*)|  true(\\*)  | Lua values   |         parent        |  join \\* | `\u003cNEVER\u003e` |\n|   true     |  false(\\*) | raise error  |         child         | `\u003cNONE\u003e` |   start   |\n|   true     |  true      | `true`       |         child         | detach   |    gc     |\n\n\n## Usage\n\n### Use custom logger\nIn this example I use [lua-log](https://github.com/moteus/lua-log) library.\n``` Lua\n-- This is child thread.\nlocal llthreads = require \"llthreads2\"\n-- Send logs using ZMQ\nlocal LOG = require\"log\".new(\n  require \"log.writer.net.zmq\".new(\"tcp://127.0.0.1:5555\")\n)\nllthread.set_logger(function(msg) LOG.error(msg) end)\n-- This error with traceback will be passed to logger\nerror(\"SOME ERROR\")\n```\n\n### Start attached thread collectd in child thread\n``` Lua \n-- This is main thread.\nlocal thread = require \"llthreads2\".new[[\n  require \"utils\".sleep(5)\n]]\n\n-- We tell that we start attached thread but child Lua State shuld be close in child thread. \n-- If `thread` became garbage in main thread then finallizer calls thread:join() \n-- and main thread may hungup. But because of child state closes in child thread all objects\n-- in this state can be destroyed and we can prevent deadlock.\nthread:start(false, false)\n\n-- We can call join.\n-- Because of Lua state destroys in child thread we can not get \n-- returned Lua values so we just returns `true`.\nthread:join()\n```\n\n### Start detached joinable thread\n``` Lua \n-- This is main thread.\nlocal thread = require \"llthreads2\".new[[\n  require \"utils\".sleep(5)\n]]\n\n-- We tell that we start detached joinable thread. In fact we start attached \n-- thread but if `thread` became garbage in main thread then finallizer just \n-- detach child thread and main thread may not hungup.\nthread:start(true, true)\n\n-- We can call join.\n-- Because of Lua state destroys in child thread we can not get \n-- returned Lua values so we just returns `true`.\nthread:join()\n```\n\n### Pass to child thread host application`s library loader\nIf you close parent Lua state then some dynamic library may be unloaded\nand cfunction in child Lua state (thread) became invalid.\n\n``` Lua \n-- `myhost.XXX` modules is built-in modules in host application\n-- host application registers cfunction as module loader\nlocal preload = {}\npreload[ 'myhost.logger' ] = package.preload[ 'myhost.logger' ]\npreload[ 'myhost.config' ] = package.preload[ 'myhost.config' ]\nllthreads.new([[\n  -- registers preload\n  local preload  = ...\n  for name, fn in pairs(preload) do package.preload[name] = fn end\n\n  local log = require 'myhost.logger'\n\n]], preload):start(true)\n```\n\n### Wait while thread is alive\n``` Lua \nlocal thread = require \"llthreads2\".new[[\n  require \"utils\".sleep(5)\n  return 1\n]]\nthread:start()\n\n-- we can not use `thread:join(0)` because we can not call it twice\n-- so all returned values will be lost\nwhile thread:alive() do \n  -- do some work\nend\n\nlocal ok, ret = thread:join() -- true, 1\n```\n\n### Use `ex` module\n``` Lua \nlocal Threads = require \"llthreads2.ex\"\n\nlocal ok, v = Threads.new(function()\n  return 1\nend):start():join()\nassert(v == 1)\n\nlocal thread = Threads.new({\n  -- this thread code gets changed arguments\n  function(a, b)\n    assert(1 == a)\n    assert(2 == b)\n    print(\"Done\")\n  end;\n  \n  -- prelude can change thread arguments\n  prelude = function(a, b)\n    assert(\"1\" == a)\n    assert(nil == b)\n    return tonumber(a), 2\n  end;\n}, \"1\")\n\nthread:start():join()\n```\n\n[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\n\n","funding_links":[],"categories":["Lua","Resources","Processes and Threads"],"sub_categories":["Multitasking","Concurrency and Multithreading"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoteus%2Flua-llthreads2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoteus%2Flua-llthreads2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoteus%2Flua-llthreads2/lists"}