How many threads do roblox servers have?

We love coroutines! Coroutines allow us to make lines of code run separately from others, and this is great when you want to wait for something and still having another bit of your script run. But just how many different coroutines can a Roblox server run at the same time? I know that for clients it’s somewhere near 4-8 threads, but Roblox servers should be way more powerful, right?

tl;dr: How many coroutines have to run at the same time for them to stop running in separate threads on server scripts?

Coroutines aren’t the same thing as hardware threads.

From https://www.lua.org/pil/9.html:

A coroutine is similar to a thread (in the sense of multithreading): a line of execution, with its own stack, its own local variables, and its own instruction pointer; but sharing global variables and mostly anything else with other coroutines. The main difference between threads and coroutines is that, conceptually (or literally, in a multiprocessor machine), a program with threads runs several threads concurrently. Coroutines, on the other hand, are collaborative: A program with coroutines is, at any given time, running only one of its coroutines and this running coroutine only suspends its execution when it explicitly requests to be suspended.

In other words, a script can make as many coroutines as memory allows. They don’t run in parallel – during execution, the VM just jumps to a new one whenever the current coroutine calls coroutine.yield() (e.g. via wait() or one of the -Async() methods).

Then, what would the memory limit be? This is really interesting

If there is a hard coded memory limit, I’m not sure it has ever been stated, nor do I feel there is a viable reason for it to be stated.

Assuming Roblox supports full coroutine compatibility (which I believe it does), there is no maximum concurrency for coroutines.

Take this for example:

coroutine.wrap(function() wait(10) print("hi 1") end)()
coroutine.wrap(function() wait(9) print("hi 2") end)()
coroutine.wrap(function() wait(8) print("hi 3") end)()
coroutine.wrap(function() wait(7) print("hi 4") end)()
coroutine.wrap(function() wait(6) print("hi 5") end)()
coroutine.wrap(function() wait(5) print("hi 6") end)()
coroutine.wrap(function() wait(4) print("hi 7") end)()
coroutine.wrap(function() wait(3) print("hi 8") end)()
coroutine.wrap(function() wait(2) print("hi 9") end)()
coroutine.wrap(function() wait(1) print("hi 10") end)()

All of these threads can run concurrently with (from my brief testing) no performance draw backs and relatively low memory usage. You will realistically never run into a coroutine limit in your coding, and if you do that’d be an issue of drastic memory drawbacks for your games other assets.

2 Likes

Roblox might release real multithreading soon (it’s on the roadmap) , it would be good to know how much threads a server will have once this is released.

2 Likes