0

In this presentation, when Stephen Clearly describing following slide (~12 minutes in the video) states that two tasks created in this code are thread-less. This sentence confuses me a lot.

enter image description here

Here are my questions.

  1. He said that task calling a Delay is thread-less and its represents a timer. However that timer must be running waiting for 2 seconds to be over. Wouldn't that timer code counting the the timer to be elapsed has to run on some thread?

  2. He then said that the task for calling the GetStringAsync API represents a network call but again whatever piece of code that's waiting on that network call to finish must be hosted somewhere. Where does that code live?

What am I missing understanding concept of task been thread-less in Async world when my thinking always been that a code always "run" under a thread.

Community
  • 1
  • 1
jim crown
  • 473
  • 3
  • 11
  • @Servy: Reading your linked answer, I can understand that all of these tasks could be performed by same thread. Still in that case, all the code is executed by some thread, I guess its the use of term Threadless in the video that is confusing me. – jim crown Mar 29 '17 at 20:57
  • No, in my example *some* of the operations are performed by a thread, and some aren't performed by *any* thread. If you need to actually do work, you need a thread, if you need to wait for something to happen, you don't need a thread at all. Instead of forcing a thread to sit there and do nothing until your operation finishes, you can just not have any thread sitting there doing nothing, and have a thread start doing work whenever the operation is finished and there is now work to do as a result. – Servy Mar 29 '17 at 20:59
  • Your question is the moral equivalent of asking how it's possible for water to boil on a stove if nobody is standing there doing nothing but watching it. The answer of course is that the stove doesn't require the work of any person to do its work. It will function perfectly fine *even if no one is there to watch it*. – Servy Mar 29 '17 at 21:01
  • The video link in my question has a Task.Delay that internally uses a timer. Is that timer running somewhere to keep track of when the time elapse? If so, where is it running? – jim crown Mar 29 '17 at 21:01
  • 2
    CPUs (and the operating systems built on top of them) have a timing mechanism built into them that's entirely unrelated to the idea of threads. You don't need a thread to sit there doing nothing for X seconds to wait X seconds. You can simply have the OS notify you of when it's been X seconds, no threads involved. – Servy Mar 29 '17 at 21:05
  • I didn't know that. I guess that's what confusing me. Any links to get more information about timing mechanism built into CPUs/OS? – jim crown Mar 29 '17 at 21:07
  • I'm sure Google has plenty. – Servy Mar 29 '17 at 21:08
  • 2
    @jimcrown: Here's a [blog article I wrote](http://blog.stephencleary.com/2013/11/there-is-no-thread.html) about how event-based tasks don't need to block a thread. – Stephen Cleary Mar 30 '17 at 12:43

0 Answers0