Skip to content
DizzasTeR edited this page Jan 28, 2021 · 3 revisions

Threads Module

Allows you to run functions on a separate thread.

Note: Only run functions on separate threads that are independent. Its also worth noting that you should ideally only run functions on separate threads which you are committed to and must finish without needing to be cancelled mid-way.

Constructor(s)

Thread:new()

Methods

  • run(function workerFunction)
  • wait() — Holds the main thread until the workerFunction thread finishes
  • get() — Gets the result of workerFunction if its ready
  • ready() — Returns true if the workerFunction has completed execution, false otherwise
  • cancelled() — Returns true if thread was destroyed using the destroy method
  • destroy() — Cancels the value produced by the workerFunction and invalidates ready, get and wait methods.

General Example

Event.bind("onServerInit", function()
    local t = Thread:new()

    t:run(function()
        local j = 0
        for i = 1, 10000000 do
            j = j + i * 500 / 300
        end
        return j
    end)

    t:destroy()

    while not t:ready() and not t:cancelled() do
        print("waiting...")
    end
    
    if not t:cancelled() then
        print(t:get())
    end

end)
Clone this wiki locally