Threading
If used poorly, threading can consume a lot of CPU cycles needlessly. If your code is multi-threaded, consider the following ways to reduce threading overhead:
If you use threads to wait for off-box resources such as databases or web services, consider using asynchronous requests instead. These are described in Chapter 6,
Do not create your own threads. This is very expensive. Instead, use
ThreadPool.QueueUserWorkItem
to get a thread from the thread pool.Do not use threads for CPU-intensive tasks. If you have four CPUs, there is no sense in having more than four CPU-intensive tasks.
Thread switches are expensive. Try to reduce the number of threads running simultaneously.
To get more information about the threads currently running, check these counters in perfmon.
Category: Thread | |
---|---|
% Processor Time |
Shows for each thread what percentage of processor time it is taking. |
Context Switches/sec |
Shows for each thread the rate of context switches per second. |