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

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)

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

    print(t:get())
end)

Clone this wiki locally