Does your Delphi application freeze during long operations?
A frozen interface during a long-running operation is the silent enemy of any modern application. This book is the definitive map for mastering concurrent programming in Delphi, guiding the reader from the fundamentals of the classic `TThread` to the mastery of the modern Parallel Programming Library (PPL).
Drawing on 30 years of experience in software architecture, this work is designed to be a complete training course, guiding both the programmer taking their first steps into threads and the architect seeking to optimize performance in high-demand scenarios.
With 548 pages, this is a complete training course that takes you from zero to a professional level. Prepare to transform your desktop, mobile, and cloud applications, delivering the performance and fluidity your users expect.
Who is This Book For?
For the Beginner Developer
If you're just starting with threads, this book is your guide. Learn the fundamentals clearly, with step-by-step examples, and build a solid foundation to avoid the most common mistakes.
For the Experienced Developer
Take your skills to the next level. Dive deep into synchronization primitives, master the PPL, and learn to apply advanced architectural patterns to build more robust and scalable applications.
For the Software Architect
Design high-performance systems. Understand the nuances of memory management, the specifics of mobile development, and the essential resilience patterns for critical systems.
What You Will Master With This Book
- End the "UI Freeze": Learn the right patterns to keep your VCL and FMX applications 100% responsive, safely delegating long-running tasks to the background.
- Prevent Concurrency Errors: Master synchronization primitives (TCriticalSection, TEvent, TMonitor) to protect shared data and avoid the dreaded deadlocks and race conditions.
- Choose the Right Tool: Understand once and for all when to use the classic TThread and when to leverage the power of the modern Parallel Programming Library (PPL) with TTask and TParallel.For.
- Build Professional Architectures: Go beyond the code and learn to design robust, maintainable concurrent systems by applying patterns like Repository, Factory, Dependency Injection, and State Machines.
- Conquer Mobile: Master the specifics of concurrency on Android and iOS, learning to avoid ANRs, process gallery images, and consume APIs in parallel without impacting the user experience.
Full Table of Contents
1: Introduction to Concurrent and Asynchronous Processing
- 1.1 - The User Interface Freeze Problem (UI Freeze)
- 1.2 - What Are Concurrent and Asynchronous Processing?
- 1.3 - A Brief History of Concurrency: From TThread to the PPL
- 1.4 - The True Goals of Concurrency
- 1.5 - The Concept of a Thread
- 1.6 - When NOT to Use Threads (and to Look for Alternatives)
2: Fundamentals of Threads in Delphi (Basic TThread)
- 2.1 - Creating and Managing Simple Threads
- 2.2 - Communicating with the Main Thread (Synchronize and Queue)
- 2.3 - Dealing with Multiple Threads and Shared Data
- 2.4 - Anonymous Threads (TThread.CreateAnonymousThread)
3: Thread Synchronization
- 3.1 - TCriticalSection - A Deeper Look at Simple Mutual Exclusion
- 3.2 - TMonitor - Synchronizing Multiple Threads
- 3.3 - TMutex - Synchronization Between Processes
- 3.4 - TSemaphore - Controlling Access to Limited Resources
- 3.5 - TEvent - Signaling Between Threads
- 3.6 - Optimizing Concurrent Access: The Readers-Writer Pattern
- 3.7 - TCountdownEvent - Synchronizing the Completion of Multiple Tasks
- 3.8 - WaitForMultipleObjects: Coordinated Waiting
4: Thread Management and Cancellation
- 4.1 - Controlled Start and Pause of Threads
- 4.2 - Graceful Thread Cancellation (Terminate and WaitFor)
- 4.3 - Cooperative Cancellation with TCancellationToken
- 4.4 - Managing Execution Priority (TThread.Priority)
- 4.5 - Exception Handling in Threads
- 4.6 - Retry Strategies in Threads
5: Asynchronous Alternatives
- 5.1 - PostMessage and SendMessage
- 5.2 - Asynchronous I/O (Overview)
- 5.3 - Integrating Asynchronous I/O with Threads
- 5.4 - Asynchronous Execution Pattern on the Main Thread
- 5.5 - Communication via System.Messaging
6: Parallel Programming Library (PPL)
- 6.1 - Introduction to the PPL
- 6.2 - The Heart of the PPL: ITask for Actions and IFuture<T> for Results
- 6.3 - TParallel.For - Parallelizing Loops
- 6.4 - Task Coordination (WaitForAll, WaitForAny)
- 6.5 - PPL Task Cancellation
- 6.6 - Other PPL Features: TParallelArray
7: Advanced Threading Topics
- 7.1 - Creating a Custom Thread Pool
- 7.2 - TInterlocked - Atomic Operations
- 7.3 - Memory Management and Multithreading
- 7.4 - Advanced PPL Management
- 7.5 - Conditional Synchronization: TConditionVariableCS
8: Best Practices and Debugging
- 8.1 - Code Organization
- 8.2 - Avoiding Concurrency with threadvar
- 8.3 - Thread-Safe Collections
- 8.4 - Preventing Deadlocks and Race Conditions
- 8.5 - Techniques for Minimizing Context Switches
- 8.6 - Debugging Multithreaded Applications
- 8.7 - Common Problems and How to Solve Them
- 8.8 - Final Recommendations
9: Threads in Mobile Applications (Android and iOS)
- 9.1 - Introduction to Concurrency on Mobile
- 9.2 - Preventing ANRs on Android
- 9.3 - Concurrency on iOS: Rules and APIs
- 9.4 - Platform-Specific Considerations
- 9.5 - Evolution of Mobile Threading Features
- 9.6 - Parallel REST Requests
- 9.7 - Reading and Processing Gallery Images
- 9.8 - Batch Processing for Maximum Speed
- 9.9 - Final Recommendations for Mobile
10: Useful Examples with PPL
- 10.1 - Parallel Processing of Multiple Files
- 10.2 - Asynchronous Network Requests with Pagination
- 10.3 - Simulations and Intensive Calculations
- 10.4 - Orchestrating Complex Workflows
- 10.5 - Task Pipeline with a State Machine
11: Practical Database Applications
- 11.1 - The Non-Negotiable Principles (The Doctrine)
- 11.2 - Essential Example: TDataModule in a TThread
- 11.3 - Optimization with FireDAC Connection Pooling
- 11.4 - Thread-less Alternative: Asynchronous Execution (amAsync)
- 11.5 - Complete Concurrent Architecture with PPL
- 11.6 - Specific Considerations for DBExpress
Appendix
- Appendix A: Quick Reference Guide to Synchronization Primitives