https://github.com/Neopallium/lua-llthreads
Low-Level threads(pthreads or WIN32 threads) for Lua.
https://github.com/Neopallium/lua-llthreads
Last synced: about 2 months ago
JSON representation
Low-Level threads(pthreads or WIN32 threads) for Lua.
- Host: GitHub
- URL: https://github.com/Neopallium/lua-llthreads
- Owner: Neopallium
- License: mit
- Created: 2011-03-24T08:26:47.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2024-10-02T10:05:59.000Z (over 1 year ago)
- Last Synced: 2025-07-30T09:46:05.531Z (5 months ago)
- Language: C
- Homepage:
- Size: 59.6 KB
- Stars: 150
- Watchers: 16
- Forks: 37
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- my-awesome-lua - lua-llthreads - Low-Level threads (pthreads and WIN32 threads) for Lua. (Resources / Multitasking)
- awesome-lua - lua-llthreads - Low-Level threads(pthreads or WIN32 threads) for Lua. (Processes and Threads)
- awesome-lua - llthreads - A simple wrapper for low-level pthreads & WIN32 threads. (Resources / Concurrency and Multithreading)
README
About
=====
[](http://travis-ci.org/Neopallium/lua-llthreads/builds)
A simple Lua wrapper for pthreads & WIN32 threads.
Each thread gets it's own `lua_State` and there is no shared global state.
The parent thread can pass data to a child thread only as parameters when creating
the child thread. The child threads can return data back to the parent thread only
when it return (i.e. ends). The parent needs to call child:join() to get the return
values from a child thread, this call will block until the child thread ends.
The design goals of this module is only provide support for creating new `lua_State`
and running them in a different thread. This module will not provide any
methods of thread-to-thread data passing between running threads (i.e. no locks, no shared state).
Thread to Thread communication methods
======================================
* The recommend method of passing data between threads is to use [ZeroMQ](http://github.com/Neopallium/lua-zmq).
* Another method is to use sockets library like [LuaSocket](http://w3.impa.br/~diego/software/luasocket) or [Nixio](http://neopallium.github.com/nixio/).
Installation
============
Release 1.2
-----------
lua-llthread 1.2 release:
$ sudo luarocks install lua-llthreads
Lastest Git Revision
--------------------
With LuaRocks 2.0.4.1:
$ sudo luarocks install https://github.com/Neopallium/lua-llthreads/raw/master/rockspecs/lua-llthreads-scm-0.rockspec
With CMake:
$ git clone git://github.com/Neopallium/lua-llthreads.git
$ cd lua-llthreads ; mkdir build ; cd build
$ cmake ..
$ make
$ sudo make install
Example usage
=============
local llthreads = require"llthreads"
local thread_code = [[
-- print thread's parameter.
print("CHILD: received params:", ...)
-- return all thread's parameters back to the parent thread.
return ...
]]
-- create detached child thread.
local thread = llthreads.new(thread_code, "number:", 1234, "nil:", nil, "bool:", true)
-- start non-joinable detached child thread.
assert(thread:start(true))
-- Use a detatched child thread when you don't care when the child finishes.
-- create child thread.
local thread = llthreads.new(thread_code, "number:", 1234, "nil:", nil, "bool:", true)
-- start joinable child thread.
assert(thread:start())
-- 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.
print("PARENT: child returned: ", thread:join())
local socket = require"socket"
socket.sleep(2) -- give detached thread some time to run.