crystal.Thread
Threads are wrappers around Lua coroutines. Functions executed in a thread can use it to trigger blocking operations, like waiting for a duration, waiting for a signal, or waiting for another thread to complete.
Threads transparently have access to all methods of the script that owns them.
Methods
| Name | Description |
|---|---|
| block | Blocks the currently running thread until this thread runs to completion or stops. |
| defer | Registers a function that will be executed when this thread runs to completion or stops. |
| hang | Blocks this thread forever. |
| is_dead | Returns whether this thread has ran to completion or stopped. |
| join | Blocks this thread until a specific thread runs to completion or stops. |
| join_any | Blocks this thread until any of several other threads runs to completion or stops. |
| script | Returns the script owning this thread. |
| stop | Stops this thread. |
| stop_on | Stops this thread whenever its parent Script receives a specific signal. |
| thread | Spawns and immediately begins executing a child thread. |
| wait | Blocks this thread for a specific duration. |
| wait_for | Blocks this thread until its parent Script receives a specific signal. |
| wait_for_any | Blocks this thread until its parent Script receives any of several signals. |
| wait_frame | Blocks this thread until the next Script:update call. |
Examples
local script = crystal.Script:new();
local my_thread = script:run_thread(function(self)
self:stop_on("bye");
self:hang();
end);
script:run_thread(function(self)
self:join(my_thread);
print("Finished joining!");
end);
script:signal("bye"); -- Prints "Finished joining!"