I believe that Roblox runs on one main (OS) thread, and there are other worker threads which I assume are also OS threads.
Does a VM only handle one thread (1 VM = 1 OS thread)? If so, do games run on a single VM because there’s only one main thread?
Do all (non-parallel) scripts run Luau threads that are scheduled to run on the main thread? What about actors, does it create a new VM and therefore use another OS thread everytime to run task.desynchronize? Does it even make a new VM? And about “use another OS thread”, it uses the worker threads right?
Is a VM is an environment where code is ran? What are threads then, a set of instructions that will be scheduled by the task scheduler under a VM?
And for Luau threads vs. OS threads, Luau threads are also considered as subthreads because they merely run under the main (OS) thread right? OS threads are the ones that are actually connected to your hardware right?
Am I using the terminology right orr am I missing something?
I don’t think OS thread is the correct term (Operating System thread?). From what I believe what you mean is, 1 VM = 1 CPU core used. So yes, there’s only 1 VM available to you if you aren’t using any Actors.
Not entirely sure what you mean here so I assume you mean if there are 2 scripts running, 1 normal and 1 Actor. However the in the Actor thread, you currently haven’t ran any code to desync it. So if that’s what you mean then yes, the code will run on main thread (main CPU core)
Yes, using an Actor creates a new VM that uses a different CPU core than the main CPU core. However task.desynchronize() doesn’t make a new thread, it just starts running code on that different CPU core while the main CPU core is also running, which runs the code in parallel.
Yes? If I’ve interpreted what you mean correctly, a VM is where code will be ran. A thread in roblox terms doesn’t actually mean a thread in the normal Computer Science terms. A thread in roblox terms is the code that is currently running that we have coded, or as you said, a set of instructions (that is currently running).
Honestly no clue what you mean here. Isn’t Luau thread the same as an OS thread? And subthread what? Threads run sequentially, meaning that it only runs 1 by 1, which I’m pretty you already know. And I don’t believe Actors can/will ever run on another devices CPU core. Unless you’re running the Actor on the Client side. Honestly not too sure about that..
I'm honestly not the best person you should be talking to about parallel coding. If I've interpreted anything you said wrong, sorry. I don't think I've said anything wrong but maybe someone smarter can show up and correct me if I am.
Oh? I’ve been seeing it tossed around as a term, here are examples of replies that use the term:
I feel like what people mean to say is that OS threads are the ones actually connected to your hardwdare (CPU), while Luau threads (like what is printed from coroutine.running()) are actually just tasks that run under OS threads.
A bit confused here, how would it make a new VM without having to make a new thread? If I’m understand this right,
RNS.RenderStepped:ConnectParallel(function()
-- on a parallel VM? ...worker thread?
task.synchronize()
-- oh back to another VM? ...main thread?
end)
Would these not make a new thread if they have different VMs and are allocated to different CPU cores? Because if it was the same thread, would it not be actual multithreading then?
Question, if a VM is an environment, then what are OS threads? Does a VM allow an OS thread to be created? Is the VM the OS thread itself? I have no idea what I’m saying, just brainstorming ghsdhds-
Switching back to serial keeps it on the same VM, although now in sequential somewhere else in the scheduler btw, the VM is bound to the actor, so if you want a new VM just create a new actor
How does that work? If they run on the same VM, how can parallel and serial Luau threads run at the same time under the same VM and assumingly, the same OS/worker thread? Is my assumption that a VM can only run a single OS/worker thread wrong?
To clarify, this is what I assume to be the case (please correct me if I’m wrong):
Each VM has an OS thread (e.g. the main thread)
Luau threads that are running serially run under that OS thread
When actors make their own VM, they use another OS thread that isn’t the main thread (worker threads)
Meaning… code running on serial is executed on the main thread, but code running on parallel is executed on one of the worker threads
I’m aware that if you open the microprofiler, you will see a main thread and then some worker threads. Are these threads connected to a single core each, which means that a core can only have one OS thread? If so, how do actors work? What happens when you have more actors than cores, like say, 64 Actors to 6 cores. This is kinda going back to my original question of, does parallel luau making a new VM mean it makes its own OS thread…? Or does it allocate to an existing OS thread…?
because they don’t work at the same time
serial doesn’t run with parallel
parallel tasks either run before or after serial
would remove the whole “serial being safe” stuff
that’s why I said somewhere else in the scheduler
Awh man.. I swear there was a really long comment on this post and I only found the free time to read Devforum right now but for some reason, it disappeared…
Was it an accidental delete? That honestly sucks… dang..
Does a VM have a single thread or multiple threads, because if it was single-threaded, are you implying that for each VM, parallel threads and serial threads dont “run at the same time”? What’s the whole point of it being called multithreaded then?
Apologies for the questions, I’m just a little confused
Each actor receives its own Luau VM.
By default, execution still continues in serial mode, running on the same CPU thread alongside other Luau threads operating in serial mode - single-threaded execution.
Once you switch to parallel mode, execution of the Luau thread is transferred to a different CPU “worker thread”. Because it’s now being ran under a different CPU thread, it will be able to run in parallel with other Luau threads which are also operating in parallel mode and that is, by definition, multi-threaded execution.
Though, since the amount of Luau threads that can exist at the same time severely exceeds the average amount of CPU threads, there’s no guarantee that your Luau thread will be the only one scheduled by the engine to execute on a specific CPU thread (not to even mention Roblox limiting the worker count to, I believe, just 3 on the client).
you’ve also forgot to mention such an attribute as ‘process niceness’
the OS regularly resorts processes and assigns a niceness tag according to how quickly the thread processes residing in are usually ready to yield; ‘nice’ ones (lower is better) get promoted to quick threads, while not-so-nice ones (such as cpu-intensive blocking algebraic calculations) are pushed up the schedule stack
I understand all the text you sent below, but my question is, “does each VM have a serial and parallel thread? ..or is it just one parallel thread? or something else entirely”
No, all code executed in serial mode is ran on one CPU thread. In contrast, code executed in parallel will be distributed across different CPU worker threads (which are not tied to a specific VM, and are shared between actors).
So what’s the relationship between a VM and a thread? Does the VM merely act as a housing for any thread? What do you mean by “worker threads not being tied to a specific VM”?
Luau threads are attached to a specific Luau VM, and said Luau VM is responsible for their execution. So yeah, you could say that it’s “housing” for threads.
As for worker threads (CPU threads), they’re determined by the engine and are not directly tied to any specific actor or VM. They’re shared by all of the VMs.
I’m trying my absolute best to explain the concept of it, but the engine just schedules execution of Luau threads, which are running in parallel mode, on different worker threads.
OHHHH, okay, yknow what, that was an oversight on my part. It’s in the name… “Luau” VM.
Correct me if I’m misunderstanding, but what you’re saying is that Luau VMs specifically handles the execution of Luau code, and this code (thread) is placed into an appropriate OS thread? And these behaviors are kinda independent from each other?
Yeah, that’s pretty much what I mean. The Luau VM interprets the bytecode & executes instructions, meanwhile the engine deals with figuring out where to schedule the thread for execution (whether it’s on the main thread, or one of the worker threads).
Genuinely curious, do you know why Roblox makes a new VM per actor? Can’t a single VM just put two Luau threads on seperate cores and run them at the same time? I’m aware that every new VM means more memory, though I have no idea as to what extent.
Luau threads are not equal to worker threads in any capacity, nor do they run themselves (they’re, as mentioned above, entirely reliant on the associated VM), so this is not possible. It has to be a separate VM. Even if it were somehow possible, reusing the same VM across multiple cores would introduce insane amounts of race conditions.