H IGH - LEVEL M ULTITHREADED P ROGRAMMING [P ART III] Primož Gabrijelčič
B ACKGROUND I NFORMATION
About Me Primož Gabrijelčič Programmer, consultant, writer, speaker – thedelphigeek.com – Hacking multithreaded code since 1999
About OmniThreadLibrary „VCL for multithreading“ Delphi 2007 – XE3[4] Open source – OpenBSD license – – omnithreadlibrary.googlecode.com Win32/Win64
About the Webinars Code and video: Code = free, video = $10 20 free books, courtesy of the De Novo Software,
High-Level Abstractions Async [/Await] Future Join ForEach ParallelTask BackgroundWorker Pipeline ForkJoin
Which abstraction to use? Consider data flow!
High-Level Abstractions Async [/Await] Future Join ForEach ParallelTask BackgroundWorker ForkJoin Pipeline Parallel iteration over data
ForEach
Iterate over … Integer range Enumerable collection (TStringList …) Thread-safe enumerable collection (OTL) Blocking collection (IOmniBlockingCollection) Anything – Custom enumerator function – Main program writing into a blocking collection
Special features Synchronized access to data Data prefetching Work stealing Optional output ordering Aggregation
aggregate := initial value for each value in source Execute(value, result) if not result.IsEmpty then Agreggator(aggregate, value)
Demos 35_ParallelFor 36_ParallelAggregate 38_OrderedFor
High-Level Abstractions Async [/Await] Future Join ForEach ParallelTask BackgroundWorker ForkJoin Pipeline Parallelization of multistage operations
Pipeline
Example
Web Spider
P ARTING N OTES
Keep in Mind Don’t parallelize everything Rethink the algorithm Data flow dictates the abstraction Measure the improvements Test, test and test
Code Will be available shortly at