Lua 5.1 pthread
Referance manual


How to use this library

This depends of LUA_CPATH and where you installed the module. Generally, you'll do thread = require("thread").

Functions provided

th = thread.create(func [ , parameters ] )
Returns a thread th created from func. The function takes in first argument the value th. The others arguments are passed by th:resume(...). The thread th do not start immediately.
It have an optional parameter, parameters which is a table that takes somes values used to configure the thread. See man pthread_attr_init. The attributes names are the keys of the table, the options must be in lowercase without any prefix.
scheduler = thread.scheduler([ yield_function ])
Return a scheduler. See the source code for more details. Not documented yet (not even tested).
th:resume(...)
thread.resume(th, ...)
Resume (or start) the thread th and give the arguments .... If the thread is not yet started, the arguments are passed as extra parameters to the function. Is the thread is yielded, the arguments are passed as return values of the function yield.
If the thread is running (started, not yielded). The arguments are placed in a stack (in the C code, the stack thread->inS). The next time the thread will call the function yield it will returns immediately with the next arguments in the input stack.
This function returns immediately, and returns nothing
It may raise a "dead_thread_error"
if the thread is dead.
... = th:result()
... = thread.result(th)
When the thread th passes values to the main thread either by running the yield function or by returning values, these values are placed in the stack (in the C code, the stack thread->outS). This function will return the arguments on this output stack. If the stack is empty, it will wait the values. Careful : if the thread is dead, this function may block forever.
th:send_result(...)
thread.send_result(th, ...)
Push the values ... on the output stack of the thread th.
th:flush( [ func ] )
thread.flush(th [ , func ] )
This function flush the souput stack of the thread th. If func is given, it must be a function is called many times with the values in the stack. If func returns false (not nil), the flush is stopped.
boolean = th:has_result()
boolean = thread.has_result(th)
This function returns true if there is values in the output stack of the thread th.
If used with alive, test alive first because the system may preempt the main thread between the two tests. If has_result may return false and the system preempt just after that, the thread may die and return results.
boolean = th:alive()
boolean = thread.alive(th)
This function returns true if the thread th is alive.
If used with has_result, test has_result after because the system may preempt the main thread between the two tests. If has_result may return false and the system preempt just after that, the thread may die and return results.
boolean = th:finnished()
boolean = thread.finnished(th)
This function returns true if the thread th is either suspended or dead (not normal).
status = th:status()
status = thread.status(th)
Retuen a string depending of the state of the thread th. One of the "normal", "running", "suspended", "dead".
... = th.yield(...)
... = thread.yield(...)
Careful : You call it by th.yield, not th:yield
Suspend the running thread, push the arguments in the output stack and wait for arguments in the input stack, then return them.
... = th:wait()
... = thread.wait(th)
Wait until the thread th pauses or finnishes. Returns a set of values in the output stack.
th:detach()
thread.detach(th)
Invoke pthread_detach
on the thread th. That's all.

Created by Mildred (website, mirror). E-mail : mildred593(at)online.fr.