What is a thread and what does task.spawn do?

I was analyzing the API, trying to find some new knowledge and trying to figure out how to apply more concepts that I already know of, and I found something that was talking about threads and something named multithreading, what do these two terms mean?

I’m also confused on what task.spawn() does, and how it works.

1 Like

A thread is a block of code that yields separately.

It’s used to yield for something and at the same time have something else not yield.

I don’t get what this means

Think of it as a road that then forks off into 2 roads. Let’s call one road Road 1, and the other road Road 2, Road 1 has nothing in its path so it continues normally without interference. Road 2 however, has to come to a stop because of incomplete construction!

Cars driving on the road come to this intersection and you can choose which car goes to road 1 and road 2. Think of Car 1 as something you want to yield, and Car 2 the rest of the script.

You steer Car 1 into Road 2, and Car 2 into Road 1.

Yield is basically road 2, it stops something, but also allows something else to continue without stopping the entire script.

3 Likes

What’s exactly the difference of using spawn() and coroutine?

1 Like

Unfortunately, I’m not sure. However, while making the script, you can type in coroutine and task.spawn() and see what comes up.

I was searching in Dev Forum for an answer and i found that Spawn() is a bit delayed while coroutines Fire things instantly

2 Likes

So coroutines > spawn ?

1 Like

It depends, i’ve heard that coroutines are difficult to debug
i like the feature that you can detect if a coroutine is dead, suspended, running
you can also resume coroutines after you stop them, which is cool
I don’t know much about Spawn, but I’m sure that they’re similar

So Basically,

coroutines

coroutines allow you to run code alongside other code, it creates a thread that runs along the main thread (The main code), Now you may be wondering: What is a thread?

I think @SubtotalAnt8185 Explained it really well,
But Basically,
They allow you to have multiple Paths in executing code within code.
Simply: They allow code run alongside the Main Code which is the Main Thread.

This is useful if you want to multi-task with things, like if you wanted a loop, you can create a a thread with a coroutine and have two things running:

local thread = coroutine.create(function() -- creates thread
    while true do
        print"Hi, I'm running alongside the main thread!"
        task.wait(.1)
    end
end)

coroutine.resume(thread) -- starts thread

while true do
    print"Hi, I'm running within the main thread!"
    task.wait(.1)
end

If you want a function thread, you would use coroutine.wrap:

local cofunc = coroutine.wrap(function(str: string)
    print(str)
end)

cofunc("Hello!") --> Hello!

task

Now, for task, task is used to fire code within the Engine Sheduler, It is basically similiar to coroutines in many ways, but to clear up one thing that has been said in this Topic:
spawn(), delay() are deperacated and should be replaced with task.spawn() and task.delay(), task.spawn() are faster and better than spawn(), which fires code immediately with the Engine’s Scheduler without throttling, while spawn() does this with throttling, task.spawn() as stated by Documentation should replace spawn()

task.spawn(function()
    print"Hello!"
end) -- this will automatically fire

task.spawn() will automatically call and resume this new thread, if you want this functionality with coroutines, you would do this for the following:

coroutine.resume(coroutine.create(function)) -- automatically fires this thread

coroutine.wrap(function)() -- automatically fires this thread

What are the Differences between the two?
Now, There real question we are asking is the Differences between these Libraries, to be honest, there are only minor Differences between the two:

  • coroutines give you more control over the thread, you are able to check its status, stop it, and resume it.

  • task threads are usually just meant to fire code, the call and fire a new thread and continue to execute the code, you can only stop it by using task.cancel(thread)
    Simply: Fire and Execute

Similarities?
task and coroutine are basically very similar if you already caught that, but here are similarities:

  • Both are used to fire code alongside the Main Thread

  • Both can fire threads and functions

  • Both are very efficient

Other than that, there are no real differences between them, they run code alongside other code, it just depends on the use cases for them.

Feel free to correct me on this information, I can always be wrong about stuff!

13 Likes

Addendum to the above, task.spawn has debug traceback (Descriptive Error Messages) unlike when using the coroutine library and allows you to pass function parameters directly:

task.spawn(print, "Hello, world!")
1 Like

Why not make this a community recource

Because at this point, there are already plenty of topic covering their differences and Similarities, Creating a Resource would just add to that.

2 Likes

Yes, but they are probably not up to date and use the spawn instead of task.spawn.

1 Like