Class threads.thread

Low-level thread creation and manipulation library.

Functions

new ( to ) Create a new thread object, or validate a thread object passed from a function.
create ( to, func, to2) Create (run) a new thread.
yield ( to ) Yield an active thread.
resume ( to ) Resume a yielded thread.
complete ( to ) Signal an active thread to complete and return.
join ( to ) Join a completed thread and free the resources.
cancel ( to ) Signal a thread to terminate and return.
id ( to ) Get a threads lua id number.
run ( to, bool ) Check threads current running state.
state ( to ) Check a threads current execution state.
sleep ( sec, nsec ) Force a thread to sleep.
exit ( to ) Force a thread to exit without returning


Functions

new ( to )
Create a new thread or validate thread passed by function.

Parameters

  • to: Thread object (optional)

Usage:

  • Create a new thread object
    local t1 = thread.create()

  • Validate a passed thread object
  • func(nt)
       local t = thread.new(nt)

    ...

Return value:

a valid thread object upon success or error if passed thread is invalid or thread creation fails
create (to, func, to2)
Create and initiate a new thread.

Parameters:

  • to: Thread Object
  • func: a lua function to run (must be global)
  • to2: parent or partner thread. (optional)

Usage:

  • Create a single thread
    threads.thread.create(t1, "func")

  • Create a single thread with access to parent/partner
    threads.thread.create(t1, "func", t2);

Return value:

0 if thread is created or non zero errno value if creation fails

See also:

Demo

yield ( to )
Yield an active thread.

Parameters

  • to: Thread Object

Usage:

Yield a thread
require "threads"
local thread = threads.thread;

function print_tid( t )
   local t = thread.new(t);
while( thread.state(t) == WORKING) do -- or t:state()
   print(t.id)
   thread.yield(t) -- or t:yield()
end

local function main(...)
   local t1 = thread.new();
   local ret = thread.create(t1, "print_tid", t2);

   for i=1, 10 do
      thread.resume(t1) -- or t1:resume()
   end
   ret = thread.join(t1);
end

Return value:

a valid thread object upon success or error if passed thread is invalid or thread creation fails.
resume ( to )
Resume a yielded thread.

Parameters

  • to: Thread Object

Return value:

0 success 1 on failure
complete ( to )
Signal a thread to complete its tasks and return.

Parameters

  • t: Thread Object

Usage:

Return value:

0 success 1 on failure
join ( to )
Join or wait on a thread to finish and free its resouces.

Parameters

  • to: Thread Object

Usage:

Return value:

0 success 1 on failure
cancel ( to )
Signal a thread to terminate and return.

Parameters

  • t: Thread Object

Usage:

Return value:

0 success 1 on failure
run ( to )
Get a lua threads running state.

Parameters

  • to: Thread Object

Usage:

Return value:

boolen representing the lua thread objects' running state.
state ( to )
Get a threads execution state. [ WORKING | SUSPENDED | ERROR | OK ]

Parameters

  • to: Thread Object

Usage:

Return value:

A number value, representing the lua thread objects' execution state.
sleep ( sec, nsec )
Force a thread to sleep for seconds or nano-seconds.

Parameters

  • sec: Integer value representing seconds to sleep
  • nsec: Integer value representing nano seconds to sleep

Usage:

Return value:

No vaule returned

See also:

exit ( to )
Force a thread to exit without returning.

Parameters

  • to: Thread Object

Usage:

Return value:

0 success 1 on failure

See also:

Valid XHTML 1.0!