Skip to content

Instantly share code, notes, and snippets.

@lucasmz-dev
Last active October 17, 2021 01:04
Show Gist options
  • Save lucasmz-dev/4317b4803780b5fcc432add74f3c49ce to your computer and use it in GitHub Desktop.
Save lucasmz-dev/4317b4803780b5fcc432add74f3c49ce to your computer and use it in GitHub Desktop.
Faster Task.Spawn
local FreeThread: thread? = nil
local function RunHandlerInFreeThread(handle, ...)
local thread = FreeThread :: thread
FreeThread = nil
handle(...)
FreeThread = thread
end
local function CreateFreeThread()
FreeThread = coroutine.running()
while true do
RunHandlerInFreeThread( coroutine.yield() )
end
end
return function(
handle: (...any) -> (),
...
)
assert(
typeof(handle) == 'function',
"Must be function"
)
if FreeThread == nil then
task.spawn(CreateFreeThread)
end
task.spawn(
FreeThread :: thread,
handle, ...
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment