The difficulty of multithreaded programming
- Difficult to reproduce failures, bugs difficult to track
- Concurrency errors are difficult to track and eliminate
- The split of a separate task is not always so clear.
- The complexity of thread synchronization and communication introduction
- Competition, deadlock, visibility issues caused by multithreading (difficult to debug, trace)
- Difficult to test, simple test does not cover production environment problems
- Poorly designed and does not fully improve performance (no control over the number of threads, resulting in meaningless context switching and cache invalidation)
Overhead introduced by multithreading
- Context switch "include cache"
- Memory synchronization/memory Barrier "synchronized and volatile in Java"
- Lock
- Memory Overhead
Multithreaded Program Optimization
- Thread pool (optimized for threading creation, destruction overhead), appropriate size
- CPU affinity (Efficient use of cache)
- Lock-free data structure
- Idle thread sleeps in time (reduces useless switching)
- Appropriate priority adjustments
About Multithreaded Program development