Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shiawaseu/threading

Luau library for managing threads and signals
https://github.com/shiawaseu/threading

Last synced: about 2 months ago
JSON representation

Luau library for managing threads and signals

Awesome Lists containing this project

README

        

# Threading
- Luau library for managing threads and signals.

## Usage

```lua
-- Declare the library
local Library = loadstring(game:HttpGet("https://raw.githubusercontent.com/shiawaseu/threading/master/Threading.luau", true))()

-- Example callback function
local function exampleCallback(...)
print("Callback:", ...)
end

-- Run a while loop thread for callback with a delay
-- local thread = Library.CreateThread(1, exampleCallback, "test")

-- Connect a callback to any Signal
local HBthread = Library.CreateThread(game:GetService("RunService").Heartbeat, exampleCallback, "Hello", "World")

-- Getting threads
local activeThreads = Library:GetThreads()
for i, thread in pairs(activeThreads) do
print("Thread:", i, thread.FunctionName)
end

print(Library:IsActiveThread(HBthread)) -- Output: true

task.wait(1)

-- Shutting down threads
HBthread:Destruct()

print(Library:IsActiveThread(HBthread)) -- Output: false
```
## Thread Object Reference
- `FunctionName `
- Function Name which was originally the callback name, `N/A` if the callback is anonymous

- `Runtime `
- The seconds that have passed since [epoch](https://en.wikipedia.org/wiki/Epoch)

- `ThreadAddressOrSignal `
- The thread the callback is running on if not a signal (delay loop) or the Signal the callback is connected to

- ` Destruct(): `
## Library API Reference
- `Library.CreateThread(SignalOrDelay: | , callback: , ...: ): `
- Creates a thread with a delay or signal. Returns a `Thread` object.

- ` Library:GetThreads(): `
- Returns a table of active threads.
- Destructed threads still remain, but no longer contain their thread/signal.

- ` Library:GetThreadCount(): `
- Returns the count of active threads.

- ` Library:IsActiveThread(thread: ) `
- Returns wether the provided `Thread` object is active & running or not.

- ` Library:GetThreadRuntime(thread: ) `
- Returns the elapsed time since the thread has started.