Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does async await increases Context switching

Tags:

I am aware of how async await works. I know that when execution reaches to await, it release the thread and after IO completes, it fetches thread from threadpool and run the remaining code. This way threads are efficiently utilized. But I am confused in some use cases:

  1. Should we use async methods for the very fast IO method, like cache read/write method? Would not they result into unnecessarily context switch. If we use sync method, execution will complete on same thread and context switch may not happen.
  2. Does Async-await saves only memory consumption(by creating lesser threads). Or it also saves cpu as well? As far as I know, in case of sync IO, while IO takes place, thread goes into sleep mode. That means it does not consume cpu. Is this understanding correct?
like image 376
Pragmatic Avatar asked Sep 30 '16 15:09

Pragmatic


People also ask

Does await cause context switch?

No. It schedules the remaining code to run on the correct context. That context might be a threadpool thread. It might be the UI thread.

What causes high context switching?

Common causes of high context switch countsToo many title threads are using the same CRITICAL_SECTION . SwitchToThread in a tight loop with other threads that are *ready to run *. Whenever another thread is ready to run, it always results in a context switch. Rapidly switching high priority threads to ready to run.

Does async await improve performance?

C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.


1 Answers

I am aware of how async await works.

You are not.

I know that when execution reaches to await, it release the thread

It does not. When execution reaches an await, the awaitable operand is evaluated, and then it is checked to see if the operation is complete. If it is not, then the remainder of the method is signed up as the continuation of the awaitable, and a task representing the work of the current method is returned to the caller.

None of that is "releasing the thread". Rather, control returns to the caller, and the caller keeps executing on the current thread. Of course, if the current caller was the only thing on this thread, then the thread is done. But there is no requirement that an async method be the only call on a thread!

after IO completes

An awaitable need not be an IO operation, but let's suppose that it is.

it fetches thread from threadpool and run the remaining code.

No. It schedules the remaining code to run on the correct context. That context might be a threadpool thread. It might be the UI thread. It might be the current thread. It might be any number of things.

Should we use async methods for the very fast IO method, like cache read/write method?

The awaitable is evaluated. If the awaitable knows that it can complete the operation in a reasonable amount of time then it is perfectly within its rights to do the operation and return a completed task. In which case there is no penalty; you're just checking a boolean to see if the task is completed.

Would not they result into unnecessarily context switch.

Not necessarily.

If we use sync method, execution will complete on same thread and context switch may not happen.

I am confused as to why you think a context switch happens on an IO operation. IO operations run on hardware, below the level of OS threads. There's no thread sitting there servicing IO tasks.

Does Async-await saves only memory consumption(by creating lesser threads)

The purpose of await is to (1) make more efficient use of expensive worker threads by allowing workflows to become more asynchronous, and thereby freeing up threads to do work while waiting for high-latency results, and (2) to make the source code for asynchronous workflows resemble the source code for synchronous workflows.

As far as I know, in case of sync IO, while IO takes place, thread goes into sleep mode. That means it does not consume cpu. Is this understanding correct?

Sure but you have this completely backwards. YOU WANT TO CONSUME CPU. You want to be consuming as much CPU as possible all the time! The CPU is doing work on behalf of the user and if it is idle then its not getting its work done as fast as it could. Don't hire a worker and then pay them to sleep! Hire a worker, and as soon as they are blocked on a high-latency task, put them to work doing something else so the CPU stays as hot as possible all the time. The owner of that machine paid good money for that CPU; it should be running at 100% all the time that there is work to be done!

So let's come back to your fundamental question:

Does async await increases Context switching

I know a great way to find out. Write a program using await, write another one without, run them both, and measure the number of context switches per second. Then you'll know.

But I don't see why context switches per second is a relevant metric. Let's consider two banks with lots of customers and lots of employees. At bank #1 the employees work on one task until it is complete; they never switch context. If an employee is blocked on waiting for a result from another, they go to sleep. At bank #2, employees switch from one task to another when they are blocked, and are constantly servicing customer requests. Which bank do you think has faster customer service?

like image 171
Eric Lippert Avatar answered Sep 17 '22 06:09

Eric Lippert