Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How different is await/async from threading?

I'm trying to familiarize myself with c#'s new await/async keywords, and I've found several aspects which I can't quite understand.

  1. Let's start with race conditions:

    Stream s=...
    ...
    for(int i=0;i<100;i++)
    {
        s.WriteAsync(new byte[]{i},0,1);
    }
    

    will this work as expected all the time (e.g. write to the file 12345..... and not 13254 or something)?

    The second thing is that async function executes synchronously if it does not contain await operator. And, according to microsoft documentation, async functions always execute in the caller thread (as compared to BeginInvoke). This brings me to 3 next questions:

  2. How much of an async function is executed before it releases to the caller function?

    async void MyAsyncFunction()
    {
        Operation1();
        Operation2();
        Operation3();
        ....
        Stream s=...;
        await s.WriteAsync(....);
    }
    

    In the articles about await/async that I've read, it's said that async functions without await operator execute sequentially, and with async/await return imminently. But it's nagging at me that MyAsyncFunction may always execute Operation1...Operation3 before releasing as it hits await s.WriteAsync.

  3. What if I use Thread.Sleep in the async function like this:

    async void DoStuff()
    {
        Stream s=...;
        ...
        await s.WriteAsync(....);
        Thread.Sleep(10000);
        ....
    }
    

    Will Thread.Sleep block the whole thread in which it is executed or just the async function?

  4. What if I use semaphore.Wait() in one of the async functions and it will expect for the semaphore to be released by an other async function. Will this behave as it would with threads, or will it cause deadlock?

  5. await does not work outside of async functions. Why?

like image 740
Arsen Zahray Avatar asked Nov 17 '12 09:11

Arsen Zahray


People also ask

Is async better than threading?

So why asyncio is faster than multi-threading if they both belong to asynchronous programming? It's because asyncio is more robust with task scheduling and provides the user with full control of code execution.

How is async different from threads?

Another example with a single-thread approach is a program that requests a file from the OS and needs to make a mathematical operation. In an asynchronous system, the program asks the OS for the file and returns the control to the mathematical operation to be executed on the CPU, while waiting for the file.

Does an asynchronous method work on a separate thread?

No, it does not. It MAY start another thread internally and return that task, but the general idea is that it does not run on any thread.

Is async better than multithreading python?

You can run multiple event loops on different threads. A single thread helps us to achieve better performance as compared to what we have achieved with multi-threading, also it is easy or clean to write async code in Python than multi-threaded code. In Python, you can use async/await syntax to write asynchronous code.


2 Answers

I recommend you read my async intro.

will this work as expected all the time (e.g. write to the file 12345..... and not 13254 or something)?

No. You need to await the calls to WriteAsync.

How much of an async function is executed before it releases to the caller function?

Until it awaits an operation that is not already completed.

Will Thread.Sleep block the whole thread in which it is executed or just the async function?

Thread.Sleep - and all other blocking methods - will block the async method and the thread that is executing it.

As a general rule, do not use any blocking methods within an async method.

What if I use semaphore.Wait() in one of the async functions and it will expect for the semaphore to be released by an other async function. Will this behave as it would with threads, or will it cause deadlock?

It totally depends on your contexts. Wait is a blocking method, so if the "other" async method requires the context held by the blocked method, then you will deadlock.

Note that SemaphoreSlim is async-friendly; you can use WaitAsync instead of Wait.

await does not work outside of async functions. Why?

Because the async keyword enables the await keyword. This was done to minimize the impact of new keywords on the C# language and for code readability.

like image 180
Stephen Cleary Avatar answered Oct 08 '22 10:10

Stephen Cleary


You can find answer for your questions about awaitoperator in the following post by Eric Lippert, where he says that:

The “await” operator … does not mean “this method now blocks the current thread until the asynchronous operation returns”. That would be making the asynchronous operation back into a synchronous operation, which is precisely what we are attempting to avoid. Rather, it means the opposite of that; it means “if the task we are awaiting has not yet completed then sign up the rest of this method as the continuation of that task, and then return to your caller immediately; the task will invoke the continuation when it completes. -- Eric Lippert

like image 15
Adrian Ciura Avatar answered Oct 08 '22 12:10

Adrian Ciura