A thread is a group of computer instructions that can be executed by a task scheduler, in particular Lua's thread task scheduler. In simple words, it is a block of code that runs in serial.
Multithreading is the ability to send multiple threads out to the scheduler and have it execute them simultaneously. It is an important part of parallel computing. When you create a new thread, that thread will seem to run at the same time as the rest of the code. This can be particularly useful for when you want to run processes that may take a long time, or perhaps even infinite time, side by side, such as important game loops.
03 | print ( "Loop a" , tick()) |
09 | print ( "Loop b" , tick()) |
A task scheduler is essentially a large loop that goes through all of the processes it has to do and executes them. It runs as fast as it possibly can, and in Lua this is a number pretty close to 0.03.
What spawn
does is wait for the current task scheduler update to end. Then, it puts in your request to make a new thread with some code content in it into the task scheduler and will wait until the next task scheduler update. When that update happens, the thread will execute and your code will begin executing. The content of the thread itself is the function you supply to spawn
.
Threads created by spawn
are different from coroutine threads. When you create a coroutine, you create a thread but you also get a reference to the thread's associated coroutine that you can manipulate with coroutine functions.
1 | local c = coroutine.create( function () |
4 | local s = spawn( function () |
spawn
does not return such a reference. It's a much more light-weight, RBX.Lua native function construct that simply queues up a new thread to the task scheduler. Once you create it, you cannot get a reference to it and you simply wait until it runs to completion.
Coroutines also run immediately and attempt to create a new thread without waiting for the current task scheduler update to complete. If you are in an environment where small increments of time like 0.03 really matter, you may want to look into coroutines over spawn.
Also note that you cannot directly pass arguments to a thread using spawn
. Calling a function within the thread with arguments is a good workaround for this.