.NET thread pool and execution context, class ExecutionContext

In chapter 27, the CLR has an instruction in the form of a C # book written by Richter:

You can use the ExecutionContext class to suppress the flow of the execution context, thereby improving the performance of your application. Profit from the preliminary preparation can be very significant for the server application.

The following are an example:

CallContext.LogicalSetData("Name", "Jeffrey");

//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
           state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));

The second part follows:

//Now suppress the flowing of the Main thread execution context
ExecutionContext.SuppressFlow();

//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
           state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));

ExecutionContext.RestoreFlow();

Result:

Name=Jeffrey
Name=

The child flow has lost its context (in the second part of the example), because the side effect parameters are lost after suppression. But we saved the processor time (in my case, about 10% of the CPU time was saved).

  • Do you use the ExecutionContext.SuppressFlow () method to reduce processor processing time?
  • , , ?
  • ?
+4

Source: https://habr.com/ru/post/1623981/


All Articles