What's a thread?

The past couple of days I’ve been perplexed with all this thread information like “thread is line of execution usually a component of a process that can be managed by a scheduler” which makes no sense to me. And others say think of a thread like a “script” and I have no clue of how to comprehend this. So if someone could plz give me a simple and clear explanation I’ll be thankful.

1 Like

A thread is basically something that will run without waiting for other lines of code to finish.

This could help:

1 Like

The answer to this varies based on context, because the Luau VM (for now) is entirely singlethreaded. This means that although Lua emulates threads through coroutines, code is not truly running in parallel. This is an important distinction, because handling threading without dealing with parallel execution guarantees that all threads will be able to reliable read/write from memory without any issues. Luau’s “threading” works primarily by executing a queued thread until something yields it (such as wait), after which it will then switch to the next queued task.
True multithreading being introduced with Parallel Lua is significantly more complicated because threads actually running in parallel won’t be able to access the same thing at the same time, which Roblox has to account for in their implementation.

3 Likes

Sometimes when people say thread they mean “coroutine”, which is a better term to google.

ROBLOX has recently been mucking around with adding a different type of threads to Lua (which @exxtremestuffs is talking about). Honestly, ignore this entire concept for now. It’s experimental, ROBLOX-specific, and does not have good documentation.

Much confusion will ensue when that release is out of beta (it might be already). But people at least for now probably generally mean “coroutine” when they say “thread”.

2 Likes

A thread is where code can run without slowing down or stoping other code(threads). Think about this: You do a while wait() do loop, it will not run the code underneath the loop. What if you need to run more code at the same time as the loop? You can use a different thread to run the code. If you go into task manager and press Performance and press CPU, you can see how many threads you have open. Here is a example:image

Man these replies use big words. I would simplify it to this. It’s not 100% correct, but it’s good enough.

A thread more or less a person doing a list of tasks. You can only do one task at a time. If you need to pick up the dog from the vet and do the shopping, you need to do one and then the other.

If you have two threads, you can ask a friend to get the groceries while you get the dog.

Lua doesn’t really have true threads, it just kind of pretends by grabbing the milk, paying the vet, grabbing the cheese, putting the dog in the car, and then grabbing the bread, so it appears to be doing both tasks at the same time.

8 Likes

Threads in coroutines are lines of codes or functions that run if it is called by something

Based on this information, I suspect a thread means some code that can run without having to wait or let other codes finish but can also run when other codes are running too?

If there’s some inaccuracy, mistake, or the need to clarify something please do so.

1 Like

Dumbed down, a thread basically lets code run at the same time as other code to allow faster programs and such.

You’re absolutely correct. A thread can do one thing at a time, and if you make another you can do two things at a time.