|
1 | 1 | # 🚀 ThreadPool for Free Pascal |
2 | 2 |
|
3 | | -[](CHANGELOG.md) |
| 3 | +[](CHANGELOG.md) |
4 | 4 | [](https://opensource.org/licenses/MIT) |
5 | 5 | [](https://www.freepascal.org/) |
6 | 6 | [](https://www.lazarus-ide.org/) |
@@ -133,9 +133,30 @@ A thread pool with fixed-size circular buffer (1024 items) and built-in backpres |
133 | 133 |
|
134 | 134 | ## 🏃 Quick Start |
135 | 135 |
|
| 136 | +> [!IMPORTANT] |
| 137 | +> **On Linux/macOS, your program must use the `cthreads` unit — and it must be the *first* unit in your program's `uses` clause.** |
| 138 | +> |
| 139 | +> Free Pascal does not install a threading manager by default on Unix-like systems. Without `cthreads`, creating the thread pool fails at runtime with an access violation (exit code 217). Windows does not need it. |
| 140 | +> |
| 141 | +> ```pascal |
| 142 | +> program MyApp; |
| 143 | +> {$mode objfpc}{$H+} |
| 144 | +> uses |
| 145 | +> {$IFDEF UNIX} |
| 146 | +> cthreads, // MUST be first on Linux/macOS |
| 147 | +> {$ENDIF} |
| 148 | +> ThreadPool.Simple; // or ThreadPool.ProducerConsumer |
| 149 | +> ``` |
| 150 | +> |
| 151 | +> The examples in this repository already include this guard — see `examples/Starter/Starter.lpr`. |
| 152 | +> |
| 153 | +> From the [official FPC documentation](https://www.freepascal.org/docs-html/rtl/cthreads/index.html): *"The cthreads unit simply needs to be included in the uses clause of the program, preferably the very first unit, and the initialization section of the unit will do all the work."* |
| 154 | +
|
136 | 155 | ### Simple Thread Pool |
137 | 156 | ```pascal |
138 | | -uses ThreadPool.Simple; |
| 157 | +uses |
| 158 | + {$IFDEF UNIX}cthreads,{$ENDIF} // see the note above — required on Linux/macOS |
| 159 | + ThreadPool.Simple; |
139 | 160 |
|
140 | 161 | // Simple parallel processing |
141 | 162 | procedure ProcessItem(index: Integer); |
|
154 | 175 |
|
155 | 176 | ### Producer-Consumer Thread Pool |
156 | 177 | ```pascal |
157 | | -uses ThreadPool.ProducerConsumer; |
| 178 | +uses |
| 179 | + {$IFDEF UNIX}cthreads,{$ENDIF} // see the note above — required on Linux/macOS |
| 180 | + ThreadPool.ProducerConsumer; |
158 | 181 |
|
159 | 182 | procedure DoWork; |
160 | 183 | begin |
@@ -361,12 +384,16 @@ All four `Queue` overloads share the same pattern — pick the one that fits you |
361 | 384 |
|
362 | 385 | For Simple Thread Pool: |
363 | 386 | ```pascal |
364 | | - uses ThreadPool.Simple; |
| 387 | + uses |
| 388 | + {$IFDEF UNIX}cthreads,{$ENDIF} // required on Linux/macOS (must be first) |
| 389 | + ThreadPool.Simple; |
365 | 390 | ``` |
366 | 391 |
|
367 | 392 | For Producer-Consumer Thread Pool: |
368 | 393 | ```pascal |
369 | | - uses ThreadPool.ProducerConsumer; |
| 394 | + uses |
| 395 | + {$IFDEF UNIX}cthreads,{$ENDIF} // required on Linux/macOS (must be first) |
| 396 | + ThreadPool.ProducerConsumer; |
370 | 397 | ``` |
371 | 398 |
|
372 | 399 | 3. Start using: |
@@ -406,6 +433,8 @@ All tasks completed successfully! |
406 | 433 |
|
407 | 434 | > [!TIP] |
408 | 435 | > Make sure your source file starts with `{$mode objfpc}{$H+}`. Without this, Free Pascal defaults to TP/Delphi-7 mode and some syntax will not compile. |
| 436 | +> |
| 437 | +> On Linux/macOS, also ensure `{$IFDEF UNIX}cthreads{$ENDIF}` is the **first** unit in your program's `uses` clause (see the [Quick Start](#-quick-start) note). Forgetting it causes a runtime access violation, not a compile error — so the build succeeds but the program crashes when it creates the pool. |
409 | 438 |
|
410 | 439 | ## ⚙️ Requirements |
411 | 440 |
|
@@ -517,7 +546,8 @@ GlobalThreadPool.WaitForAll; |
517 | 546 | ``` |
518 | 547 |
|
519 | 548 | ## 🚧 Planned/In Progress |
520 | | -- Adaptive thread adjustment based on a load factor |
| 549 | + |
| 550 | +- Richer error handling — collect all task errors (not just the last) and an optional `OnError` callback (planned for 0.7.0) |
521 | 551 | - Support for `procedure Queue(AMethod: TProc; AArgs: array of Const);` |
522 | 552 | - More comprehensive tests |
523 | 553 | - More examples |
|
0 commit comments