Difference between the TPL & async/await (Thread handling)

28,320

Solution 1

I believe the TPL (TaskFactory.Startnew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool.

Pretty much.

From what i've been reading it seems like async/await only "sometimes" creates a new thread.

Actually, it never does. If you want multithreading, you have to implement it yourself. There's a new Task.Run method that is just shorthand for Task.Factory.StartNew, and it's probably the most common way of starting a task on the thread pool.

If you were dealing with IO completion ports i can see it not having to create a new thread but otherwise i would think it would have to.

Bingo. So methods like Stream.ReadAsync will actually create a Task wrapper around an IOCP (if the Stream has an IOCP).

You can also create some non-I/O, non-CPU "tasks". A simple example is Task.Delay, which returns a task that completes after some time period.

The cool thing about async/await is that you can queue some work to the thread pool (e.g., Task.Run), do some I/O-bound operation (e.g., Stream.ReadAsync), and do some other operation (e.g., Task.Delay)... and they're all tasks! They can be awaited or used in combinations like Task.WhenAll.

Any method that returns Task can be awaited - it doesn't have to be an async method. So Task.Delay and I/O-bound operations just use TaskCompletionSource to create and complete a task - the only thing being done on the thread pool is the actual task completion when the event occurs (timeout, I/O completion, etc).

I guess my understanding of FromCurrentSynchronizationContext always was a bit fuzzy also. I always throught it was, in essence, the UI thread.

I wrote an article on SynchronizationContext. Most of the time, SynchronizationContext.Current:

  • is a UI context if the current thread is a UI thread.
  • is an ASP.NET request context if the current thread is servicing an ASP.NET request.
  • is a thread pool context otherwise.

Any thread can set its own SynchronizationContext, so there are exceptions to the rules above.

Note that the default Task awaiter will schedule the remainder of the async method on the current SynchronizationContext if it is not null; otherwise it goes on the current TaskScheduler. This isn't so important today, but in the near future it will be an important distinction.

I wrote my own async/await intro on my blog, and Stephen Toub recently posted an excellent async/await FAQ.

Regarding "concurrency" vs "multithreading", see this related SO question. I would say async enables concurrency, which may or may not be multithreaded. It's easy to use await Task.WhenAll or await Task.WhenAny to do concurrent processing, and unless you explicitly use the thread pool (e.g., Task.Run or ConfigureAwait(false)), then you can have multiple concurrent operations in progress at the same time (e.g., multiple I/O or other types like Delay) - and there is no thread needed for them. I use the term "single-threaded concurrency" for this kind of scenario, though in an ASP.NET host, you can actually end up with "zero-threaded concurrency". Which is pretty sweet.

Solution 2

async / await basically simplifies the ContinueWith methods ( Continuations in Continuation Passing Style )

It does not introduce concurrency - you still have to do that yourself ( or use the Async version of a framework method. )

So, the C# 5 version would be:

await Task.Run( () => DoSomeAsyncWork() );
DoSomeWorkAfter();
Share:
28,320
coding4fun
Author by

coding4fun

Updated on July 05, 2022

Comments

  • coding4fun
    coding4fun almost 2 years

    Trying to understanding the difference between the TPL & async/await when it comes to thread creation.

    I believe the TPL (TaskFactory.StartNew) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool. That's of course unless you use TaskCreationOptions.LongRunning which creates a new thread.

    I thought async/await would work similarly so essentially:

    TPL:

    Factory.StartNew( () => DoSomeAsyncWork() )
    .ContinueWith( 
        (antecedent) => {
            DoSomeWorkAfter(); 
        },TaskScheduler.FromCurrentSynchronizationContext());
    

    Async/Await:

    await DoSomeAsyncWork();  
    DoSomeWorkAfter();
    

    would be identical. From what I've been reading it seems like async/await only "sometimes" creates a new thread. So when does it create a new thread and when doesn't it create a new thread? If you were dealing with IO completion ports i can see it not having to create a new thread but otherwise I would think it would have to. I guess my understanding of FromCurrentSynchronizationContext always was a bit fuzzy also. I always throught it was, in essence, the UI thread.

  • coding4fun
    coding4fun almost 12 years
    so where is it running DoSomeAsyncWork (async/wait version) in my example above? If its running on the UI thread how does it not block?
  • Nick Butler
    Nick Butler almost 12 years
    Your await example won't compile if DoSomeWorkAsync() returns void or something that isn't awaitable. From your first example, I assumed that it is a sequential method that you want to run on a different thread. If you changed it to return a Task, without introducing concurrency, then yes it would block. In the sense that it would execute sequentially and be just like normal code on the UI thread. await only yields if the method returns an awaitable that hasn't yet completed.
  • Michael Ray Lovett
    Michael Ray Lovett over 11 years
    well, I wouldn't say it runs wherever it chooses to run. You've used Task.Run to execute the code in DoSomeAsyncWork, so in this case your work will be done on a threadpool thread.
  • Philippe
    Philippe over 9 years
    Nice answer. I would also recommend infoq.com/articles/Async-API-Design and this excellent presentation: channel9.msdn.com/Events/TechEd/Europe/2013/DEV-B318.
  • Felipe Deveza
    Felipe Deveza over 6 years
    First link is dead.
  • RBT
    RBT about 4 years
    I loved the succinctness of your answer.
  • Artemious
    Artemious over 3 years
    "async/await FAQ" link is dead
  • Stephen Cleary
    Stephen Cleary over 3 years
    Yup. Microsoft moved a bunch of stuff around and broke pretty much all links to their content. This answer accepts PRs.