I have built an application in C# that I would like to be optimized for multiple cores. I have some threads, should I do more?
Updated for more detail
- C# 2.0
- Run on Windows Vista and Windows Server 2003
Updated again
- This code is running as a service
- I do not want to have the complete code... my goal here is to get your experience and how to start. Like I say, I have already use threads. What more can I do?
I'd generalize that writing a highly optimized multi-threaded process is a lot harder than just throwing some threads in the mix.
I recommend starting with the following steps:
- Split up your workloads into discrete parallel executable units
- Measure and characterize workload types - Network intensive, I/O intensive, CPU intensive etc - these become the basis for your worker pooling strategies. e.g. you can have pretty large pools of workers for network intensive applications, but it doesn't make sense having more workers than hardware-threads for CPU intensive tasks.
- Think about queuing/array or ThreadWorkerPool to manage pools of threads. Former more finegrain controlled than latter.
- Learn to prefer async I/O patterns over sync patterns if you can - frees more CPU time to perform other tasks.
- Work to eliminate or atleast reduce serialization around contended resources such as disk.
- Minimize I/O, acquire and hold minimum level of locks for minimum period possible. (Reader/Writer locks are your friend)
5.Comb through that code to ensure that resources are locked in consistent sequence to minimize deadly embrace.
- Test like crazy - race conditions and bugs in multithreaded applications are hellish to troubleshoot - often you only see the forensic aftermath of the massacre.
Bear in mind that it is entirely possible that a multi-threaded version could perform worse than a single-threaded version of the same app. There is no excuse for good engineering measurement.