Best Practices in Multithreaded Windows Programming
This 2 or 3-day lecture-lab course (the length is customized to client requirements) reviews core topics and emphasizes advanced topics, including performance, scalability, and parallelism on multiprocessor systems.
Extensive hands-on exercises (the exercises are optional; some
clients prefer demonstrations), using Microsoft Visual Studio, allow
participants to develop, enhance, and debug practical and realistic
multithreaded applications that illustrate the topics discussed in the lectures.
Complete solutions are provided in all cases, and many of the solutions can be
used as the foundation for future application development. All programs run on
both 32-bit and 64-bit systems, and Windows 7 features are exploited as
appropriate.
The course and the lab exercises concentrate on the core issues and do not use GUI interfaces, although graphical user interfaces can be added to the code. Many of the topics are appropriate when building servers.
Audience: Software engineers and programmers, including those with some experience with threads who need to learn how to develop reliable, high-performance multithreaded Windows client, desktop, and server applications.
Prerequisites: Knowledge of C (or C++, C#, Java, etc.)
programming, experience developing Windows applications with Microsoft Visual
Studio (helpful, but not required). Prior experience with Windows thread
management and synchronization is helpful, but not required.
COURSE SCHEDULE
There are 3-4 sessions per day, depending on the pace; each session consists of a lecture with demonstrations followed by a lab. Average session time is 90 minutes. A one-hour lunch break is recommended. Each day normally runs 7.5 hours (including lunch); for example, 9:00 – 4:30. The instructor will be available for as long as required after each session.
Session 1. Windows Thread Management
- Process and Thread Overview
- Thread Management
- Waiting for Thread Termination
- The C Library and Threads
- Demonstration: Building a Threaded Application
- Thread Priority, Scheduling, multiprocessor Impact
- Debugging Techniques - Introduction (There is additional coverage
during the exercises
Session 2. Windows Synchronization
- Need for Synchronization
- Thread Synchronization Objects
- CRITICAL_SECTIONs
- Deadlocks, Race Conditions, and Other Defects
- Interlocked Functions
- Mutexes
- Events
- Semaphores
- Slim Reader/Writer (SRW) Locks
- Condition
Variables
Session 3. Performance Impact and Guidelines
- CRITICAL_SECTION – Mutex Tradeoffs
- Multiprocessor Impact
- Locks and Performance
Impact. Minimizing Lock Usage
- Slim Reader/Writer (SRW) Locks and Condition Variales
- Processor Affinity
- Tuning with CS Spin Counts
- Performance Guidelines and Pitfalls
Session 4. Thread Safety, DLLs, and Thread-Local Storage
- Multithreading Persistent State Problem
- The DllMain function
- Thread Local Storage
- TLS-DLL Demonstration: Message-Based Socket communication
- Thread-Safe Libraries w/o TLS: State Parameters
Session 5. Threading Models for Reliability
and Performance
- Multithreading Models
- Data Parallelism and File/Stream Processing
- Computational Parallelism
- Condition Variable Usage
- Thread Pools
- More Performance Tradeoffs
- Avoiding Incorrect Code - Hints and Guidelines
Session 6. Compound Object Examples and
Exercises
- Threshold Barrier Problem
- A Queue Object and Pipelines
- Demo: Multiple Wait Semaphore with Process-Shared Objects
- Demo: Batons to Serialize Thread Execution
Session 7. Advanced Windows Threading Features
(Optional)
- Asynchronous Procedure Calls
- Alertable Wait Functions
- APCs and the Baton Example
- Waitable Timers
- Asynchronous I/O Quick Overview
- Extended (Callback) I/O NT6 (Vista, Windows 7, ...) Performance
Session 8. Introduction to Pthreads for Application Portability
(Optional)
- The Pthreads Model and Standard
- The Open Source Windows Pthreads Library and UNIX/Linux Portability
- Pthreads Thread Management and Synchronization Introduction
- Performance Comparisons
- Pros and Cons