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 code syntax 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 REST APIs in parallel without harming user experience.
Complete Table of Contents
1: Introduction to Concurrent and Async Processing
- 1.1 - The UI Freeze Problem
- 1.2 - What is Concurrent and Asynchronous Processing?
- 1.3 - A Brief History: From TThread to PPL
- 1.4 - The Real Goals of Concurrency
- 1.5 - The Concept of Thread
- 1.6 - When NOT to Use Threads
2: Delphi Thread Fundamentals (Basic TThread)
- 2.1 - Creating and Managing Simple Threads
- 2.2 - Communicating with the Main Thread (Synchronize and Queue)
- 2.3 - Handling Multiple Threads and Shared Data
- 2.4 - Anonymous Threads (TThread.CreateAnonymousThread)
3: Thread Synchronization
- 3.1 - TCriticalSection - Deep Dive into Simple Mutual Exclusion
- 3.2 - TMonitor - Multi-Thread Synchronization
- 3.3 - TMutex - Inter-Process Synchronization
- 3.4 - TSemaphore - Resource Access Control
- 3.5 - TEvent - Signaling Between Threads
- 3.6 - Optimizing Concurrent Access: Reader-Writer Pattern
- 3.7 - TCountdownEvent - Multi-Task Completion Synchronization
- 3.8 - WaitForMultipleObjects: Coordinated Waiting
4: Thread Management and Cancellation
- 4.1 - Controlled Thread Start and Pause
- 4.2 - Gentle 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 and Reprocessing Strategies
5: Asynchronous Alternatives
- 5.1 - PostMessage and SendMessage
- 5.2 - Asynchronous I/O Overview
- 5.3 - Integrating Async I/O with Threads
- 5.4 - Async Execution Pattern on Main Thread
- 5.5 - Communication via System.Messaging
6: Parallel Programming Library (PPL)
- 6.1 - Introduction to PPL - Task-Based Programming
- 6.2 - Core PPL: ITask for Actions and IFuture<T> for Results
- 6.3 - TParallel.For - Parallelizing Loops
- 6.4 - Task Coordination (WaitForAll, WaitForAny)
- 6.5 - Canceling PPL Tasks
- 6.6 - Additional PPL Features: TParallelArray
7: Advanced Thread Topics
- 7.1 - Building a Custom Thread Pool
- 7.2 - TInterlocked - Atomic Operations for Extreme Performance
- 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 - Shared Data and Thread-Safe Collections
- 8.4 - Deadlock and Race Condition Prevention
- 8.5 - Techniques to Minimize Context Switches
- 8.6 - Debugging Multithreaded Applications
- 8.7 - Common Pitfalls and How to Solve Them
- 8.8 - Final Recommendations and Conclusions
9: Threads in Mobile Applications (Android and iOS)
- 9.1 - Introduction to Mobile Concurrency
- 9.2 - Preventing ANRs on Android
- 9.3 - iOS Concurrency: 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 Mobile Recommendations
10: Practical Examples with PPL
- 10.1 - Parallel Multi-File Processing
- 10.2 - Async Network Requests with Pagination
- 10.3 - Simulations and Intensive Calculations
- 10.4 - Complex Workflow Orchestration
- 10.5 - Task Pipeline with State Machine
11: Practical Database Applications
- 11.1 - Non-Negotiable Principles
- 11.2 - Essential Example: TDataModule in a TThread
- 11.3 - Optimization with FireDAC Connection Pooling
- 11.4 - Threadless Alternative: Async Execution (amAsync)
- 11.5 - Complete Concurrent Architecture with PPL
- 11.6 - Specific Considerations for DBExpress
Appendix
- Appendix A: Quick Guide to Synchronization Primitives
Professional Web Development with Delphi and Dext Framework
Build RESTful APIs, gRPC channels, and high-performance cloud-native microservices with Clean Architecture in Delphi. (Portuguese edition out now; English edition coming soon!)
View Book Details →