Delphi Multithreading (English edition)
Writed by : Cesar Romero
Published date : 03/11/2025
ISBN-10 : 6501779057
ISBN-13 : 9786501779058
Language : English
Web site : https://www.cesarromero.com.br/index_en.html
A frozen interface during a long-running operation is the silent enemy of any modern application. In the Delphi universe, mastering concurrent programming is the key to transforming slow and frustrating applications into fluid, responsive, and professional systems. However, the path of multithreading is often seen as a minefield of deadlocks, race conditions, and complexities that intimidate even the most experienced developers.
This book is the definitive map for that journey. Drawing on 30 years of experience in software architecture, Cesar Romero demystifies concurrency in Delphi, guiding the reader from the fundamentals of the classic TThread to mastering the modern Parallel Programming Library (PPL). Written with a didactic approach focused on the “why” behind each decision, 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.
Throughout its practical chapters, you will learn to:
- Manage the thread lifecycle safely, avoiding the most common errors.
- Master synchronization primitives, choosing the right tool for each challenge.
- Simplify your code and fully leverage multi-core processors with TTask, IFuture, and TParallel.For
- Diagnose and debug the most complex concurrency problems.
- Apply best practices to real-world examples, including concurrent database access and the development of fluid mobile applications for Android and iOS.
By the end of this book, you will not only have a repertoire of code but also the confidence and architectural vision to design and build Delphi applications that don’t just work—they delight users with their performance and responsiveness.
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: Responsiveness, Scalability, and Performance
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 (Introduction to Synchronization)
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 with a Single Object
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 on Multiple Events
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 (PostMessage / SendMessage, Asynchronous I/O)
5.1: PostMessage and SendMessage - Asynchronous and Synchronous Communication via Windows Messages
5.2: Asynchronous I/O (Overview)
5.3: Integrating Asynchronous I/O with Threads
5.4: Asynchronous Execution Pattern on the Main Thread: The TMainThreadDispatcher
5.5: Communication Between Threads and Platforms via System.Messaging
6: Parallel Programming Library (PPL) - Simplifying Concurrency
6.1: Introduction to the PPL - The Leap to Task-Based Programming
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: TTask.WaitForAll, TTask.WaitForAny, and TParallel.Join
6.5: PPL Task Cancellation (ITask.Cancel and ITask.CheckCanceled)
6.6: Other PPL Features: TParallelArray
7: Advanced Topics in Threads
7.1: Creating a Custom Thread Pool
7.2: TInterlocked - Atomic Operations for Extreme Performance
7.3: Memory Management and Multithreading
7.4: Advanced PPL Management: TThreadPool and TThreadPoolStats
7.5: Conditional Synchronization: TConditionVariableCS
8: Best Practices and Debugging
8.1: Code Organization (Threads in Separate Units)
8.2: Avoiding Concurrency with threadvar (Thread-Local Storage)
8.3: Shared Data and Thread-Safe Collections: Ensuring Integrity in Multithreaded Applications
8.4: Preventing Common Problems: Deadlocks and Race Conditions
8.5: Techniques to Minimize Context Switches
8.6: Debugging Multithreaded Applications
8.7: Common Problems in Multithreaded Applications and How to Solve Them
8.8: Final Recommendations and Conclusions for Best Practices
9: Threads and Concurrency in Mobile Applications (Android and iOS)
9.1: Introduction to Concurrency in Mobile Applications
9.2: Specifics of the UI Thread on Mobile and Preventing ANRs on Android
9.3: Concurrency on iOS: Rules and Native APIs
9.4: Platform-Specific Considerations and Life Cycle Management
9.5: Evolution of Threading Features for the Mobile Platform with Delphi
9.6: Parallel REST Requests to Public APIs
9.7: Reading Images from the Photo Gallery in a PPL Task
9.8: Batch Processing for Maximum Throughput
9.9: Final Recommendations for Mobile Concurrency
10: Useful Examples with the PPL
10.1: Parallel Processing of Multiple Files in a Batch
10.2: Asynchronous Network Requests with Pagination
10.3: Intensive Data Simulations and Calculations in Parallel
10.4: Orchestrating Complex Workflows with Dependencies
10.5: Task Pipeline with a State Machine
11: Practical Database Applications with Concurrency
11.1: The Non-Negotiable Principles: The Doctrine
11.2: Essential Practical Example: TDataModule in a TThread
11.3: Performance Optimization with FireDAC Connection Pooling
11.4: An Alternative Without Threads: Asynchronous Execution (amAsync) in FireDAC
11.5: Building a Complete Concurrent Architecture with the PPL, Databases, and Design Patterns
11.6: Specific Considerations for DBExpress