Async and Await - How is order of execution maintained?

前端 未结 2 1605
迷失自我
迷失自我 2021-02-08 21:13

I am actually reading some topics about the Task Parallel Library and the asynchronous programming with async and await. The book \"C# 5.0 in a Nutshell\" states that when await

2条回答
  •  礼貌的吻别
    2021-02-08 21:52

    Just for the sake of simplicity, I'm going to replace your example with one that's slightly simpler, but has all of the same meaningful properties:

    async Task DisplayPrimeCounts()
    {
        for (int i = 0; i < 10; i++)
        {
            var value = await SomeExpensiveComputation(i);
            Console.WriteLine(value);
        }
        Console.WriteLine("Done!");
    }
    

    The ordering is all maintained because of the definition of your code. Let's imagine stepping through it.

    1. This method is first called
    2. The first line of code is the for loop, so i is initialized.
    3. The loop check passes, so we go to the body of the loop.
    4. SomeExpensiveComputation is called. It should return a Task very quickly, but the work that it'd doing will keep going on in the background.
    5. The rest of the method is added as a continuation to the returned task; it will continue executing when that task finishes.
    6. After the task returned from SomeExpensiveComputation finishes, we store the result in value.
    7. value is printed to the console.
    8. GOTO 3; note that the existing expensive operation has already finished before we get to step 4 for the second time and start the next one.

    As far as how the C# compiler actually accomplishes step 5, it does so by creating a state machine. Basically every time there is an await there's a label indicating where it left off, and at the start of the method (or after it's resumed after any continuation fires) it checks the current state, and does a goto to the spot where it left off. It also needs to hoist all local variables into fields of a new class so that the state of those local variables is maintained.

    Now this transformation isn't actually done in C# code, it's done in IL, but this is sort of the morale equivalent of the code I showed above in a state machine. Note that this isn't valid C# (you cannot goto into a a for loop like this, but that restriction doesn't apply to the IL code that is actually used. There are also going to be differences between this and what C# actually does, but is should give you a basic idea of what's going on here:

    internal class Foo
    {
        public int i;
        public long value;
        private int state = 0;
        private Task task;
        int result0;
        public Task Bar()
        {
            var tcs = new TaskCompletionSource();
            Action continuation = null;
            continuation = () =>
            {
                try
                {
                    if (state == 1)
                    {
                        goto state1;
                    }
                    for (i = 0; i < 10; i++)
                    {
                        Task task = SomeExpensiveComputation(i);
                        var awaiter = task.GetAwaiter();
                        if (!awaiter.IsCompleted)
                        {
                            awaiter.OnCompleted(() =>
                            {
                                result0 = awaiter.GetResult();
                                continuation();
                            });
                            state = 1;
                            return;
                        }
                        else
                        {
                            result0 = awaiter.GetResult();
                        }
                    state1:
                        Console.WriteLine(value);
                    }
                    Console.WriteLine("Done!");
                    tcs.SetResult(true);
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            };
            continuation();
        }
    }
    
    
    

    Note that I've ignored task cancellation for the sake of this example, I've ignored the whole concept of capturing the current synchronization context, there's a bit more going on with error handling, etc. Don't consider this a complete implementation.

    提交回复
    热议问题