Cerike
Cerike

Reputation: 364

Does Task.Delay make sense on a dedicated thread?

I know that using Thread.Sleep in a task is a dumb idea, because it will block that thread in the thread pool. However, if I have a thread dedicated to something, created by new Thread(new ThreadStart(Foo)), does Task.Delay still make sense? Nothing is going to use that thread anyhow. I am also wondering what exact effect a Task.Delay will have. It won't put the thread to sleep, so will it act as a spin wait? That would be much worse then a sleep. Wouldn't it be?

Upvotes: 2

Views: 654

Answers (1)

Eric Lippert
Eric Lippert

Reputation: 660004

I am having difficulty figuring out from the question what the scenario here is; it seems to suggest that you are allocating a thread and you then wish it to do nothing for some amount of time. That's a strange thing to do, to pay for a worker to do nothing. Clarify the scenario please.

To answer your specific questions:

It won't put the thread to sleep, so will it act as a spin wait?

No.

That would be much worse then a sleep. Wouldn't it be?

Yes, it would be terrible! It would have all the down sides of sleeping a thread but would consume power and make heat along the way. You'd not just be paying the overhead of allocating the thread, you'd be actively wasting power for no benefit. The only time you ever want to spin wait is when you are waiting for something that will happen in significantly less time than the thread quantum. Spin wait for nanoseconds or worst case, for microseconds, not for milliseconds. You want to spin wait for things that take a few instructions, and we run billions of instructions per second.

Based on your question, my suspicion is (1) you don't understand at all what Task.Delay does, and (2) you are allocating a thread unnecessarily.

Let's first say what Task.Delay does.

Task.Delay immediately returns an object that represents the task of delaying for some number of milliseconds.

That's all it does.

Really.

It does not actually delay for any number of milliseconds. It returns a task that represents the concept of delaying. Make sure that is clear in your head.

If you ask the task whether it is complete or not, it will tell you whether that number of seconds have passed, of course.

If you assign some function as the continuation of that task, that continuation will be scheduled to run on the current context at some point after the task is complete.

What then does await Task.Delay(whatever); do?

I just said what it does. Task.Delay returns a task that represents a delay. await checks to see if that task is complete. If it is, then the program continues as normal. If the task is not complete then the location of the await becomes the continuation point of the task, and the method returns to its caller. The continuation is then scheduled to execute on the current context at some point in the future after the task completes.

Why you would hire a worker in order to do this, I do not know or understand. Hire workers to do jobs that are CPU intensive, where they do not stop hammering on the CPU for a nanosecond until they are done. Do not hire workers to do things that delay for milliseconds at a time, like accessing disks or delaying work!

Upvotes: 9

Related Questions