Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...