21

Important for anyone researching this difficult topic in Unity specifically,

be sure to see another question I asked which raised related key issues:

In Unity specifically, "where" does an await literally return to?


For C# experts, Unity is single-threaded1

It's common to do calculations and such on another thread.

When you do something on another thread, you often use async/wait since, uh, all the good C# programmers say that's the easy way to do that!

void TankExplodes() {

    ShowExplosion(); .. ordinary Unity thread
    SoundEffects(); .. ordinary Unity thread
    SendExplosionInfo(); .. it goes to another thread. let's use 'async/wait'
}

using System.Net.WebSockets;
async void SendExplosionInfo() {

    cws = new ClientWebSocket();
    try {
        await cws.ConnectAsync(u, CancellationToken.None);
        ...
        Scene.NewsFromServer("done!"); // class function to go back to main tread
    }
    catch (Exception e) { ... }
}

OK, so when you do this, you do everything "just as you normally do" when you launch a thread in a more conventional way in Unity/C# (so using Thread or whatever or letting a native plugin do it or the OS or whatever the case may be).

Everything works out great.

As a lame Unity programmer who only knows enough C# to get to the end of the day, I have always assumed that the async/await pattern above literally launches another thread.

In fact, does the code above literally launch another thread, or does c#/.Net use some other approach to achieve tasks when you use the natty async/wait pattern?

Maybe it works differently or specifically in the Unity engine from "using C# generally"? (IDK?)

Note that in Unity, whether or not it is a thread drastically affects how you have to handle the next steps. Hence the question.


Issue: I realize there's lots of discussion about "is await a thread", but, (1) I have never seen this discussed / answered in the Unity setting (does it make any difference? IDK?) (2) I simply have never seen a clear answer!


1 Some ancillary calculations (eg, physics etc) are done on other threads, but the actual "frame based game engine" is one pure thread. (It's impossible to "access" the main engine frame thread in any way whatsoever: when programming, say, a native plugin or some calculation on another thread, you just leave markers and values for the components on the engine frame thread to look at and use when they run each frame.)

20
  • it depends on the platform and the scripting backend. Commented Mar 29, 2019 at 18:03
  • 1
    @0xBFE1A8 ah I see. Fascinating! Commented Mar 29, 2019 at 19:03
  • 1
    On iOS Unity uses GCD library. Commented Mar 29, 2019 at 19:06
  • 1
    Therefore Most of the writing about threads in Unity is incorrect! Commented Mar 29, 2019 at 19:26
  • 1
    At least in Unity 2020.3, we have observed that tasks in Android do trigger new threads by default. We tested with an otherwise empty project with a tiny script that starts a task and reports debug logs in and out of it, and also attempt to get certain properties like DataPath. While it didn't complain in windows, in Android, Debug.Logs from tasks did not show up in the log (which matches the behaviour for Debug.Log from a separate thread. Additionally getting the DataPath complained that we were not in the main thread. Again, this only happened in Android, not windows when we tested it. Commented Nov 14, 2022 at 1:06

7 Answers 7

23

This reading: Tasks are (still) not threads and async is not parallel might help you understand what's going on under the hood. In short in order for your task to run on a separate thread you need to call

Task.Run(()=>{// the work to be done on a separate thread. }); 

Then you can await that task wherever needed.

To answer your question

"In fact, does the code above literally launch another thread, or does c#/.Net use some other approach to achieve tasks when you use the natty async/wait pattern?"

No - it doesn't.

If you did

await Task.Run(()=> cws.ConnectAsync(u, CancellationToken.None));

Then cws.ConnectAsync(u, CancellationToken.None) would run on a separate thread.

As an answer to the comment here is the code modified with more explanations:

    async void SendExplosionInfo() {

        cws = new ClientWebSocket();
        try {
            var myConnectTask = Task.Run(()=>cws.ConnectAsync(u, CancellationToken.None));

            // more code running...
await myConnectTask; // here's where it will actually stop to wait for the completion of your task. 
            Scene.NewsFromServer("done!"); // class function to go back to main tread
        }
        catch (Exception e) { ... }
    }

You might not need it on a separate thread though because the async work you're doing is not CPU bound (or so it seems). Thus you should be fine with

 try {
            var myConnectTask =cws.ConnectAsync(u, CancellationToken.None);

            // more code running...
await myConnectTask; // here's where it will actually stop to wait for the completion of your task. 
            Scene.NewsFromServer("done!"); // continue from here
        }
        catch (Exception e) { ... }
    }

Sequentially it will do exactly the same thing as the code above but on the same thread. It will allow the code after "ConnectAsync" to execute and will only stop to wait for the completion of "ConnectAsync" where it says await and since "ConnectAsync" is not CPU bound you (making it somewhat parallel in a sense of the work being done somewhere else i. e. networking) will have enough juice to run your tasks on, unless your code in "...." also requires a lot of CPU bound work, that you'd rather run in parallel.

Also you might want to avoid using async void for it's there only for top level functions. Try using async Task in your method signature. You can read more on this here.

Sign up to request clarification or add additional context in comments.

9 Comments

Wait - look at your last sentence. Does it mean: , "... would run on a separate thread. And then when that is finished the next line of code (so, the "...") would then run." Am I right ??!
This is true but i dont recommend using this because if ya use many threads all you gonna see is performance drops because of Unitys UnitySynchronizationContext its better to use jobs to be honest async and await is meh currently(for separate threads)
@Fattie If you await it right there, then yes, it will wait for that thread to finish and the task to complete. If you want things to run in parallel then you should assign that task to a variable and then await it wherever you want. I'll put more info in my answer but you should be able to get that info from the link I posted.
@Menyus , a good point. One thing though, performance is rarely the issue: when doing Unity programming it's (absolutely) essential to know whether you're still on the Unity thread or not.
@agfc - wait !!!!!!!!!!!!!! in this amazing system. In you first code example where it uses Task.Run to specifically make a new thread. Eventually it "comes back" at the "await myConnectTask". In fact, does it know to "come back" ON THE ORIGINAL THREAD ? If so - holy crap, that is clever !!!!!!!!!
|
5

No, async/await does not mean - another thread. It can start another thread but it doesn't have to.

Here you can find quite interesting post about it: https://blogs.msdn.microsoft.com/benwilli/2015/09/10/tasks-are-still-not-threads-and-async-is-not-parallel/

5 Comments

Ah ,thanks - that's from an actual MSFT guy right. Damn, I have to read that more carefully - it's still confusing!
It will never start another thread. await doesn't execute anything by itself, it awaits already executing asynchronous operations without blocking the current thread
@Fattie it's not confusing - await awaits, it doesn't start anything. Adding async to a method signature won't make it asynchronously by magic.
@PanagiotisKanavos - fantastic, got it. You should pick up Unity, you'd make a fortune by actually understanding c# :)
@Fattie once it clicks in your head you can produce pretty efficient code by properly using a combination of async and parallel code.
4

I don't like answering my own question, but as it turns out none of the answers here is totally correct. (However many/all of the answers here are hugely useful in different ways).

In fact, the actual answer can be stated in a nutshell:

On which thread the execution resumes after an await is controlled by SynchronizationContext.Current.

That's it.

Thus in any particular version of Unity (and note that, as of writing 2019, they are drastically changing Unity - https://unity.com/dots) - or indeed any C#/.Net environment at all - the question on this page can be answered properly.

The full information emerged at this follow-up QA:

https://stackoverflow.com/a/55614146/294884

Comments

3
+150

Important notice

First of all, there's an issue with your question's first statement.

Unity is single-threaded

Unity is not single-threaded; in fact, Unity is a multi-threaded environment. Why? Just go to the official Unity web page and read there:

High-performance multithreaded system: Fully utilize the multicore processors available today (and tomorrow), without heavy programming. Our new foundation for enabling high-performance is made up of three sub-systems: the C# Job System, which gives you a safe and easy sandbox for writing parallel code; the Entity Component System (ECS), a model for writing high-performance code by default, and the Burst Compiler, which produces highly-optimized native code.

The Unity 3D engine uses a .NET Runtime called "Mono" which is multi-threaded by its nature. For some platforms, the managed code will be transformed into native code, so there will be no .NET Runtime. But the code itself will be multi-threaded anyway.

So please, don't state misleading and technically incorrect facts.

What you're arguing with, is simply a statement that there is a main thread in Unity which processes the core workload in a frame-based way. This is true. But it isn't something new and unique! E.g. a WPF application running on .NET Framework (or .NET Core starting with 3.0) has a main thread too (often called the UI thread), and the workload is processed on that thread in a frame-based way using the WPF Dispatcher (dispatcher queue, operations, frames etc.) But all this doesn't make the environment single-threaded! It's just a way to handle the application's logic.


An answer to your question

Please note: my answer only applies to such Unity instances that run a .NET Runtime environment (Mono). For those instances that convert the managed C# code into native C++ code and build/run native binaries, my answer is most probably at least inaccurate.

You write:

When you do something on another thread, you often use async/wait since, uh, all the good C# programmers say that's the easy way to do that!

The async and await keywords in C# are just a way to use the TAP (Task-Asynchronous Pattern).

The TAP is used for arbitrary asynchronous operations. Generally speaking, there is no thread. I strongly recommend to read this Stephen Cleary's article called "There is no thread". (Stephen Cleary is a renowned asynchronous programming guru if you don't know.)

The primary cause for using the async/await feature is an asynchronous operation. You use async/await not because "you do something on another thread", but because you have an asynchronous operation you have to wait for. Whether there is a background thread this operation will run or or not - this does not matter for you (well, almost; see below). The TAP is an abstraction level that hides these details.

In fact, does the code above literally launch another thread, or does c#/.Net use some other approach to achieve tasks when you use the natty async/wait pattern?

The correct answer is: it depends.

  • if ClientWebSocket.ConnectAsync throws an argument validation exception right away (e.g. an ArgumentNullException when uri is null), no new thread will be started
  • if the code in that method completes very quickly, the result of the method will be available synchronously, no new thread will be started
  • if the implementation of the ClientWebSocket.ConnectAsync method is a pure asynchronous operation with no threads involved, your calling method will be "suspended" (due to await) - so no new thread will be started
  • if the method implementation involves threads and the current TaskScheduler is able to schedule this work item on a running thread pool thread, no new thread will be started; instead, the work item will be queued on an already running thread pool thread
  • if all thread pool threads are already busy, the runtime might spawn new threads depending on its configuration and current system state, so yes - a new thread might be started and the work item will be queued on that new thread

You see, this is pretty much complex. But that's exactly the reason why the TAP pattern and the async/await keyword pair were introduced into C#. These are usually the things a developer doesn't want to bother with, so let's hide this stuff in the runtime/framework.

@agfc states a not quite correct thing:

"This won't run the method on a background thread"

await cws.ConnectAsync(u, CancellationToken.None);

"But this will"

await Task.Run(()=> cws.ConnectAsync(u, CancellationToken.None));

If ConnectAsync's synchronous part implementation is tiny, the task scheduler might run that part synchronously in both cases. So these both snippets might be exactly the same depending on the called method implementation.

Note that the ConnectAsync has an Async suffix and returns a Task. This is a convention-based information that the method is truly asynchronous. In such cases, you should always prefer await MethodAsync() over await Task.Run(() => MethodAsync()).

Further interesting reading:

2 Comments

@Fattie, I undeleted my answer. I previously deleted it because it only applies to those Unity instances that run a .NET Runtime. I have no generic answer for all Unity variants, including those transforming the C# code into native.
For anyone here, note that unity's ad copy in the quoted "High-performance multithreaded system" is: stupid :) Unity runs on 1 thread. Obviously, self-evidently, "you" can as the programmer of course launch as many threads as you want "in c#", just as in any software at all. (The "C# Job System" they mention is little more than a convenience wrapper for that.) But unity itself, the game engine (ie, the frame based system) is and remains utterly single-threaded. All updates and components run on the one thread. (The other two things they mention in the ad copy are totally irrelevant.)
0
+300

The code after an await will continue on another threadpool thread. This can have consequences when dealing with non-thread-safe references in a method, such as a Unity, EF's DbContext and many other classes, including your own custom code.

Take the following example:

    [Test]
    public async Task TestAsync()
    {
        using (var context = new TestDbContext())
        {
            Console.WriteLine("Thread Before Async: " + Thread.CurrentThread.ManagedThreadId.ToString());
            var names = context.Customers.Select(x => x.Name).ToListAsync();
            Console.WriteLine("Thread Before Await: " + Thread.CurrentThread.ManagedThreadId.ToString());
            var result = await names;
            Console.WriteLine("Thread After Await: " + Thread.CurrentThread.ManagedThreadId.ToString());
        }
    }

The output:

------ Test started: Assembly: EFTest.dll ------

Thread Before Async: 29
Thread Before Await: 29
Thread After Await: 12

1 passed, 0 failed, 0 skipped, took 3.45 seconds (NUnit 3.10.1).

Note that code before and after the ToListAsync is running on the same thread. So prior to awaiting any of the results we can continue processing, though the results of the async operation will not be available, just the Task that is created. (which can be aborted, awaited, etc.) Once we put an await in, the code following will be effectively split off as a continuation, and will/can come back on a different thread.

This applies when awaiting for an async operation in-line:

    [Test]
    public async Task TestAsync2()
    {
        using (var context = new TestDbContext())
        {
            Console.WriteLine("Thread Before Async/Await: " + Thread.CurrentThread.ManagedThreadId.ToString());
            var names = await context.Customers.Select(x => x.Name).ToListAsync();
            Console.WriteLine("Thread After Async/Await: " + Thread.CurrentThread.ManagedThreadId.ToString());
        }
    }

Output:

------ Test started: Assembly: EFTest.dll ------

Thread Before Async/Await: 6
Thread After Async/Await: 33

1 passed, 0 failed, 0 skipped, took 4.38 seconds (NUnit 3.10.1).

Again, the code after the await is executed on another thread from the original.

If you want to ensure that the code calling async code remains on the same thread then you need to use the Result on the Task to block the thread until the async task completes:

    [Test]
    public void TestAsync3()
    {
        using (var context = new TestDbContext())
        {
            Console.WriteLine("Thread Before Async: " + Thread.CurrentThread.ManagedThreadId.ToString());
            var names = context.Customers.Select(x => x.Name).ToListAsync();
            Console.WriteLine("Thread After Async: " + Thread.CurrentThread.ManagedThreadId.ToString());
            var result = names.Result;
            Console.WriteLine("Thread After Result: " + Thread.CurrentThread.ManagedThreadId.ToString());
        }
    }

Output:

------ Test started: Assembly: EFTest.dll ------

Thread Before Async: 20
Thread After Async: 20
Thread After Result: 20

1 passed, 0 failed, 0 skipped, took 4.16 seconds (NUnit 3.10.1).

So as far as Unity, EF, etc. goes, you should be cautious about using async liberally where these classes are not thread safe. For instance the following code may lead to unexpected behaviour:

        using (var context = new TestDbContext())
        {
            var ids = await context.Customers.Select(x => x.CustomerId).ToListAsync();
            foreach (var id in ids)
            {
                var orders = await context.Orders.Where(x => x.CustomerId == id).ToListAsync();
                // do stuff with orders.
            }
        }

As far as the code goes this looks fine, but DbContext is not thread-safe and the single DbContext reference will be running on a different thread when it is queried for Orders based on the await on the initial Customer load.

Use async when there is a significant benefit to it over synchronous calls, and you're sure the continuation will access only thread-safe code.

4 Comments

Thanks again - FTR in fact I have written a related question! stackoverflow.com/questions/55613081/…
You answer is not correct. The code after an await will continue on another threadpool thread - this is wrong. In fact, the continuation depends on that thread's SynchronizationContext where the code before await runs. E.g. in a WinForms or a WPF app, the code after await will continue (by default) on the same thread where the code before await was executed. In your example it is not the case, because you have no SynchronizationContext. But Unity has a SynchronizationContext on its main thread, so code after await will continue on that thread, not on the thread pool thread.
So it does, that was news to me. It effectively means that it is doing the same as example #3 behind the scenes, though if you are writing your own async methods you still need to be aware that they are executing in a separate thread, just with a sync context the resumption would come back to the original thread. (I.e. keep async methods functional)
Thanks! It's good to see that it sounds like the behaviour is under control.
0

Unity's Monobehavior scripts which are derived from this library using UnityEngine;: are all single threaded and none of the API functions of UnityEngine is threadsafe so they can't be called from another thread and must run on the main thread.

However ScriptableObject and ECS (Entity component system) based APIs and DOTS (Data Oriented technology stacks) which are based on these libraries using Unity; using Unity.Entities using Unity.Mathematics using Unity.Physics: are all threadsafe APIs which can be jobbified or used with a IJobEntity or Isystem with ideometic foreach or Systembase with Entities.Foreach are threadsafe and can be run on all threads and use 100% of all of your CPU cores.

  • If you need maximum performance use ECS with DOTS jobbify everything or use ideometic foreach in an Isystem to iterate between entities which share specific components use EntityQueries as array for entities and use NativeArray instead of array to store array of data like int or float.
  • avoid using strings at all costs. use enums instead.
  • use EntityCommandBuffer to spawn entities.
  • use ComputeShader based animations for 3D characters.
  • Never use async await or Thread or Task or any other managed nullable classes.
  • Always use unmanaged non_nullable structs and store data for your components using IComponentData , ISharedComponentData and DynamicBuffers.
  • never use any managed thing inside IComponentData , ISharedComponentData and DynamicBuffers.
  • use Burst compiler and make sure all of your methods are burst compiled by adding this attribute one line before your methods : [BurstCompile]

3 Comments

Howdy - while a valuable essay, this really doesn't answer the question. Also - is this ChatGPT ??
no its not ChatGPT or AI.
it does not exactly answer this question but this question is totally wrong since Unity is completely different from general CSharp and multithreading in Unity is based on Jobs while multithreading in general CSharp is based on Thread class. also async await are not supposed to start a new thread in general CSharp let alone in Unity.
-1

I'm amazed there is no mention of ConfigureAwait in this thread. I was looking for a confirmation that Unity did async/await the same way that it is done for "regular" C# and from what I see above it seems to be the case.

The thing is, by default, an awaited task will resume in the same threading context after completion. If you await on the main thread, it will resume on the main thread. If you await on a thread from the ThreadPool, it will use any available thread from the thread pool. You can always create different contexts for different purposes, like a DB access context and whatnot.

This is where ConfigureAwait is interesting. If you chain a call to ConfigureAwait(false) after your await, you are telling the runtime that you do not need to resume in the same context and therefore it will resume on a thread from the ThreadPool. Omitting a call to ConfigureAwait plays it safe and will resume in the same context (main thread, DB thread, ThreadPool context, whatever the context the caller was on).

So, starting on the main thread, you can await and resume in the main thread like so:

    // Main thread
    await WhateverTaskAync();
    // Main thread

or go to the thread pool like so:

    // Main thread
    await WhateverTaskAsync().ConfigureAwait(false);
    // Pool thread

Likewise, starting from a thread in the pool:

    // Pool thread
    await WhateverTaskAsync();
    // Pool thread

is equivalent to :

    // Pool thread
    await WhateverTaskAsync().ConfigureAwait(false);
    // Pool thread

To go back to the main thread, you would use an API that transfers to the main thread:

    // Pool thread
    await WhateverTaskAsync().ConfigureAwait(false);
    // Pool thread
    RunOnMainThread(()
    {
        // Main thread
        NextStep()
    });
    // Pool thread, possibly before NextStep() is run unless RunOnMainThread is synchronous (which it normally isn't)

This is why people say that calling Task.Run runs code on a pool thread. The await is superfluous though...

   // Main Thread
   await Task.Run(()
   {
       // Pool thread
       WhateverTaskAsync()
       // Pool thread before WhateverTaskAsync completes because it is not awaited
    });
    // Main Thread before WhateverTaskAsync completes because it is not awaited

Now, calling ConfigureAwait(false) does not guarantee that the code inside the Async method is called in a separate thread. It only states that when returning from the await, you have no guarantee of being in the same threading context.

If your Async method looks like this:

private async Task WhateverTaskAsync()
{
    int blahblah = 0;

    for(int i = 0; i < 100000000; ++i)
    {
        blahblah += i;
    }
}

... because there is actually no await inside the Async method, you will get a compilation warning and it will all run within the calling context. Depending on its ConfigureAwait state, it might resume on the same or a different context. If you want the method to run on a pool thread, you would instead write the Async method as such:

private Task WhateverTaskAsync()
{
    return Task.Run(()
    {
        int blahblah = 0;

        for(int i = 0; i < 100000000; ++i)
        {
            blahblah += i;
        }
    }
}

Hopefully, that clears up some things for others.

1 Comment

Howdy - while a valuable essay, this really doesn't answer the question (I think!) Also, who the heck would downvote this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.