44
55# ThreadPool for Free Pascal
66
7- [ ![ Version] ( https://img.shields.io/badge/version-0.8.5 -8B5CF6.svg )] ( CHANGELOG.md )
7+ [ ![ Version] ( https://img.shields.io/badge/version-0.9.0 -8B5CF6.svg )] ( CHANGELOG.md )
88[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-1E3A8A.svg )] ( LICENSE.md )
99[ ![ Free Pascal] ( https://img.shields.io/badge/Free%20Pascal-3.2.2+-3B82F6.svg )] ( https://www.freepascal.org/ )
1010[ ![ Lazarus] ( https://img.shields.io/badge/Lazarus-4.0+-60A5FA.svg )] ( https://www.lazarus-ide.org/ )
@@ -19,11 +19,12 @@ producer-consumer workloads that need backpressure.
1919
2020[ Quick start] ( #quick-start ) · [ Cheat sheet] ( docs/CHEATSHEET.md ) ·
2121[ API documentation] ( #documentation ) · [ Examples] ( examples/ ) ·
22- [ v0.8.5 release notes] ( docs/release-notes-v0.8.5 .md )
22+ [ v0.9.0 release notes] ( docs/release-notes-v0.9.0 .md )
2323
2424> [ !TIP]
25- > ✨ ** New in v0.8.5:** refreshed project identity, a shorter README, and a new
26- > [ cheat sheet] ( docs/CHEATSHEET.md ) . Runtime behavior is unchanged from v0.8.0.
25+ > ✨ ** New in v0.9.0:** observable task handles, task batches, efficient
26+ > chunked ranges, and pending-work cancellation. Existing v0.8 queueing code
27+ > remains source-compatible. See the [ task API] ( docs/ThreadPool.Tasks-API.md ) .
2728
2829> [ !NOTE]
2930> This library is designed for simple parallel processing and learning-friendly
@@ -47,6 +48,9 @@ Both implementations provide:
4748- event-driven workers with no polling sleeps;
4849- four task forms: procedures, methods, and indexed variants;
4950- timeout-aware ` TryQueue ` and ` WaitForAll ` overloads;
51+ - observable ` Submit ` /` TrySubmit ` task handles;
52+ - task batches and chunked ` SubmitRange ` processing;
53+ - race-safe cancellation of work that has not started;
5054- deterministic, draining ` Shutdown ` ;
5155- captured worker exceptions through ` LastError ` , ` Errors ` , and ` OnError ` ; and
5256- automatic worker-count selection with safety limits.
@@ -130,6 +134,33 @@ end.
130134The first constructor argument is the worker count; ` 0 ` selects
131135` TThread.ProcessorCount ` . The second is queue capacity.
132136
137+ ### Tasks, batches, and ranges
138+
139+ Add ` ThreadPool.Tasks ` when work needs to be observed or coordinated:
140+
141+ ``` pascal
142+ uses
143+ ThreadPool.Tasks, ThreadPool.Simple;
144+
145+ var
146+ Task: IThreadPoolTask;
147+ Batch: IThreadPoolTaskBatch;
148+ begin
149+ Task := GlobalThreadPool.Submit(@DoWork);
150+ if Task.WaitFor(250) and (Task.State = ttsFailed) then
151+ WriteLn(Task.ErrorMessage);
152+
153+ Batch := GlobalThreadPool.SubmitRange(@ProcessItem, 0, 999);
154+ Batch.WaitFor;
155+ end;
156+ ```
157+
158+ ` Task.Cancel ` succeeds only while the task is pending. It never interrupts a
159+ running callback. A range uses a small number of chunks by default instead of
160+ creating one queue item per index. See the
161+ [ task API] ( docs/ThreadPool.Tasks-API.md ) for batch counts, timeouts, explicit
162+ chunk sizes, and bounded-pool rules.
163+
133164## Lifecycle and timeouts
134165
135166Both pools follow one monotonic lifecycle:
@@ -173,6 +204,9 @@ expires.
173204> also have no automatic execution deadline; add cancellation or
174205> application-level timeouts where needed.
175206
207+ Task handles and batches do not retain their pool. They may be kept after a
208+ pool is freed, because pool destruction drains accepted work first.
209+
176210## Error handling
177211
178212Task exceptions are caught so a worker failure does not terminate the pool.
@@ -226,12 +260,30 @@ Requirements:
226260
227261## Examples
228262
263+ Build every example in Release mode from the repository root:
264+
265+ ``` powershell
266+ .\build-examples.ps1
267+ ```
268+
269+ ``` sh
270+ sh ./build-examples.sh
271+ ```
272+
273+ Both scripts discover ` examples/*/*.lpi ` automatically and place the
274+ executables in the ignored root-level ` example-bin/ ` directory. Pass ` Default `
275+ to build the default mode, or use ` -Rebuild ` in PowerShell / ` --rebuild ` in the
276+ shell script to force a complete rebuild.
277+
229278| Start with | Demonstrates |
230279| --- | --- |
231280| [ ` Starter ` ] ( examples/Starter/ ) | Smallest compilable program with explanatory comments |
232281| [ ` SimpleDemo ` ] ( examples/SimpleDemo/ ) | Procedures, methods, indexes, and the global pool |
233282| [ ` ProdConSimpleDemo ` ] ( examples/ProdConSimpleDemo/ ) | Basic bounded-pool ownership and queueing |
234283| [ ` SimpleErrorHandlingBasic ` ] ( examples/SimpleErrorHandlingBasic/ ) | Reading captured errors after completion |
284+ | [ ` TaskCoordination ` ] ( examples/TaskCoordination/ ) | Task handles, batches, ranges, and cancellation |
285+ | [ ` CoordinatedFileBackup ` ] ( examples/CoordinatedFileBackup/ ) | Per-file progress, critical-failure policy, and pending cancellation |
286+ | [ ` ParallelLogAnalyzer ` ] ( examples/ParallelLogAnalyzer/ ) | Chunked analysis followed by a parallel reporting phase |
235287
236288More focused samples cover:
237289
@@ -241,8 +293,11 @@ More focused samples cover:
241293 [ ` SimpleWordCounter ` ] ( examples/SimpleWordCounter/ ) , and
242294 [ ` ProdConMessageProcessor ` ] ( examples/ProdConMessageProcessor/ ) ;
243295- advanced callbacks: [ ` SimpleErrorHandling ` ] ( examples/SimpleErrorHandling/ ) ;
244- - real I/O: [ ` ParallelFileHasher ` ] ( examples/ParallelFileHasher/ ) and
245- [ ` ParallelUrlFetcher ` ] ( examples/ParallelUrlFetcher/ ) .
296+ - real I/O: [ ` ParallelFileHasher ` ] ( examples/ParallelFileHasher/ ) ,
297+ [ ` ParallelUrlFetcher ` ] ( examples/ParallelUrlFetcher/ ) , and
298+ [ ` CoordinatedFileBackup ` ] ( examples/CoordinatedFileBackup/ ) ;
299+ - coordinated data processing:
300+ [ ` ParallelLogAnalyzer ` ] ( examples/ParallelLogAnalyzer/ ) .
246301
247302## Documentation
248303
@@ -251,9 +306,10 @@ More focused samples cover:
251306| [ Cheat sheet] ( docs/CHEATSHEET.md ) | Calls and safety rules at a glance |
252307| [ Simple API] ( docs/ThreadPool.Simple-API.md ) | Complete unbounded-pool reference |
253308| [ Producer-Consumer API] ( docs/ThreadPool.ProducerConsumer-API.md ) | Complete bounded-pool reference |
309+ | [ Tasks API] ( docs/ThreadPool.Tasks-API.md ) | Submit, wait, batch, range, and cancellation contracts |
254310| [ Simple technical guide] ( docs/ThreadPool.Simple-Technical.md ) | Internal design and synchronization |
255311| [ Producer-Consumer technical guide] ( docs/ThreadPool.ProducerConsumer-Technical.md ) | Queue and backpressure internals |
256- | [ v0.8.5 release notes] ( docs/release-notes-v0.8.5 .md ) | Current release scope and compatibility |
312+ | [ v0.9.0 release notes] ( docs/release-notes-v0.9.0 .md ) | Current release scope and compatibility |
257313| [ Changelog] ( CHANGELOG.md ) | Full version history |
258314
259315The banner's editable source is
0 commit comments