Samples
On this page
Each sample is a complete console program: it creates its own scheduler, its own input files or network peers, checks its results and cleans up after itself. Every sample on these pages is compiled and run against the current API, so the code you read is the code that works.
All samples
| Sample | What it shows |
|---|---|
| Hello task | The smallest program: a scheduler, one task, a wait, a clean shutdown. |
| Ping-pong | Two tasks hand the processor back and forth through two semaphores; round trips per second. |
| Wait for a group | Fork/join: hundreds of short tasks summing an array, joined with a reusable task group. |
| Producer/consumer | Work items through a bounded lock-free queue, counted by semaphores, sized so it cannot overflow. |
| Timeout | Waits with a time limit, sleeping tasks, tasks started by time and a cancelled start. |
| Two schedulers | Requests on a scheduler of their own hand long computations to a batch scheduler and still start on time. |
| Async file copy | Overlapped reads and writes from a task, with the next read running while the current block is written. |
| Hashing | A file streamed through a zero-copy pipe into a SHA-256 hash while it is still being read. |
| Ordered output | A file of records built front to back: deferred copies by copy tasks, writing by a writer task. |
| TCP echo server | A listening socket, one task per connection, and clients in the same program that check the echo. |
| Bluetooth devices | WinRT device searches and lookups as tasks, waited for with WinRT::Await from ordinary functions. |
To build a sample, set up a project as described in
Set up a project and add the sample's .cpp file. The
samples run on Windows and Linux, except Hashing, Bluetooth devices and the UI-thread and
thread-callback guides, which use Windows APIs; some need an extra Windows library, and their pages
say which. On Linux the repository's CMakeLists.txt builds every sample that runs there.
Real-world task graphs
The samples are small on purpose. Larger programs built on ConcurrentTasks show how the same building blocks scale to complete task graphs – readers, encoders and writers running as many tasks that wait on each other, on files and on memory pipes:
- ctLame – an MP3 encoder that writes the same files as LAME (source).
- ctJpeg – a JPEG encoder that writes the same files as jpegli (source).
- ctOpus – an Opus encoder that writes the same files as opusenc (source).
- ctDeflate – a gzip compressor that writes the same files as libdeflate's gzip (source).
- ctJxl – a JPEG XL encoder that writes the same files as cjxl from libjxl; cjxl already runs in parallel, and ctJxl makes it faster by ordering its work differently (preliminary; source, task graph).
- ctJolt – the physics engine Jolt Physics with its physics step running as fine-grained tasks; every step ends with exactly the same state as unmodified Jolt (preliminary, Windows only; source, task graph).
Each showcase page tells how its task graph evolved and what it gained; for ctJxl and ctJolt, their task graph documents on GitHub do.
ctLame is also the worked example of the chapter Task-based programming: A real program: ctLame derives its tasks and shared objects from LAME's loop, step by step, with excerpts of its source.
The sources are in the ThinkMeta.ConcurrentTasks.Samples repository on GitHub, one folder per program. The Task graphs guide explains the patterns they use.