Created
November 28, 2012 18:55
-
-
Save Buildstarted/4163247 to your computer and use it in GitHub Desktop.
Testing how cpu intensive operations are handled via TPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| static void Main() | |
| { | |
| //This method will cause the CPU to be at 100% | |
| //However it will only create enough threads | |
| //based on the # of cores in your system. | |
| //(As long as you don't pass LongRunning as an option | |
| Run(SpinWait); | |
| //This method releases the CPU and waits | |
| //20 seconds for each child thread. If you specify | |
| //LongRunning in the options it will create all | |
| //the threads it can. | |
| Run( | |
| Sleep, | |
| /* specifies that this task is long running but not cpu intensive | |
| * this will tell the task manager to start up as many as possible | |
| * if the operation is cpu intensive it has the tendency to over | |
| * subscribe the cpus with too many threads | |
| */ | |
| TaskCreationOptions.LongRunning | |
| ); | |
| Console.WriteLine("Press any key to continue"); | |
| Console.ReadKey(); | |
| } | |
| private static void Run( | |
| Action action, | |
| TaskCreationOptions options = | |
| /* force the parent to wait until all children are completed before completing itself */ | |
| TaskCreationOptions.AttachedToParent | |
| ) | |
| { | |
| //attach the option if not provided | |
| if (!options.HasFlag(TaskCreationOptions.AttachedToParent)) | |
| { | |
| options |= TaskCreationOptions.AttachedToParent; | |
| } | |
| var parent = Task.Factory.StartNew(() => | |
| { | |
| object lockObject = new object(); | |
| int count = 0; | |
| Console.WriteLine("Parent thread started"); | |
| for (int i = 0; i < 50; i++) | |
| { | |
| Task.Factory.StartNew((index) => | |
| { | |
| lock (lockObject) | |
| { | |
| count++; | |
| Console.WriteLine("Thread {1} started (Total Thread count: {0})", count, index); | |
| } | |
| action(); | |
| lock (lockObject) | |
| { | |
| count--; | |
| Console.WriteLine("Thread {1} finished (Total Thread count: {0})", count, index); | |
| } | |
| }, | |
| /* index parameter of startNew */ | |
| i, | |
| options | |
| ); | |
| } | |
| }); | |
| parent.Wait(); | |
| Console.WriteLine("Parent thread completed"); | |
| } | |
| /// <summary> | |
| /// Waits the maximum amount of cycles necessary - DO NOT USE with TaskCreationOptions.LongRunning | |
| /// This option will max out your CPU usage | |
| /// </summary> | |
| private static void SpinWait() | |
| { | |
| Thread.SpinWait(int.MaxValue); | |
| } | |
| /// <summary> | |
| /// Sleeps for 20 seconds OK to use with TaskCreationOptions.LongRunning | |
| /// </summary> | |
| private static void Sleep() | |
| { | |
| Thread.Sleep(20000); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment