Parallel Luau library designed to make parallel luau easier to use (cause the default parallel luau api is shit)
Requires and runs the given module on a separate CPU thread, passing any given arguments. Returns the id of the spawned thread.
-
Type
function thread.spawn(execute_module: ModuleScript, ...): number
-
Usage
local sharedTable = SharedTable.new() --Not necessary, but must be used to return any data. local runningThreads = {} for i = 1, 16 do table.insert(runningThreads, thread.spawn(execute_module, sharedTable, i) --Extra arguments can be passed through like a function end
Yields the calling coroutine until the given thread(s) has finished executing (if it isn't allready finished).
-
Type
function thread.join(thread_id: number | { number })
-
Usage
--Waits for all running threads to finish for _, v in runningThreads do thread.join(v) end
--Sugar syntax for the previous code block thread.join(runningThreads)
The library takes in modules in place of functions since functions cannot be cross-loaded between Luau VMs, meaning modules must be used in place of functions.
-
Type
When passing a module through the module must only return a function as such:
return function(...) end
If you want to return any data from running parallel code you must use SharedTables; as regular tables cannot be used to return data, and using regular returns is impossible with the new api.
- Type
local sharedTable = SharedTable.new() local t = thread.spawn(execute_module, sharedTable)