Concurrency for C++
This library was created based on my solutions for problems from the MIPT concurrency course. Major part of tests comes from the same course.
Original design (including this readme): await by Roman Lipovsky.
- Scalable work-stealing scheduler –
executors::tp::fast::ThreadPool - Transparent stackful fibers via
executors::fibers::ThreadPoolwith automatic pooling - Functional futures with almost everything deduced in compile-time
- Futures are lazy ⟹ allocation and synchronization is minimized (usually to zero)
- Transparent cooperative cancellation, wait-free for sequential composition and lock-free for parallel composition
- Structured concurrency via parallel combinators ('All', 'First', etc)
- Deterministic stress-tests with fault-injection via
Twistframework
- Executors
- Thread Pools (
ThreadPool)tp::compute::ThreadPoolwith shared blocking queue for independent CPU-bound tasks- Scalable work-stealing
tp::fast::ThreadPoolfor fibers / stackless coroutines (IO-bound tasks)
Strand(asynchronous mutex)ManualExecutorfor deterministic testing- Transparent fibers (
fibers)fibers::ThreadPoolfibers::ManualExecutor
- Thread Pools (
- Futures
- Types (
types)- concepts
SomeFuture,Future<T>,EagerFuture BoxedFuture<T>
- concepts
- Constructors (
make)AfterContract+Promise<T>FailureJustNeverSubmitValue
- Combinators (
combine)- Sequential composition (
seq)Map/AndThen/OrElse– monadic mappers forResult<T>Flatten– flattens nested futures (for asynchronous mappers)FlatMap=Map+FlattenVia– sets executor and/or hint for following mappersBox– erases concreteFuture(Thunk) typeStart(orForce) – converts toEagerFuture, starts operationFork<N>– splits future value into N copiesOnComplete/OnCancel/Anyway– add side effectWithTimeout– attaches timeout
- Parallel composition (
par)All/BothFirstSelect(std::variantalternative toFirstwhich records first finished future (even if there was an error))Quorum(blocking)no_allocversions which saves up allocations at the cost of less intuitive semantics
- Sequential composition (
- Terminators (
run)Await– synchronously unwrapsResultfrom futureDetach– moves future on the stack and starts it without cancelling itDiscard– moves future on the stack, starts it and cancels it
- Operators (
syntax)- pipe:
Just() | Via(pool) | Apply([]{}) - bang:
!f~f | Start() - or:
f or g~FirstOf(f, g) - sum:
f + g~All(f, g)
- pipe:
- Types (
- Stackful Fibers
- Scheduling (
sched)YieldSleepFor
- Synchronization (
sync)Mutex(lock-free)OneShotEvent(lock-free)WaitGroup(lock-free)- Buffered
Channel<T>+Select - Lock-free unbuffered
experimental::Channel<T>
- Scheduling (
- Timers
| Supported | |
|---|---|
| Architecture | x86-64 |
| Operating System | Linux |
| Compiler | Clang++ (≥ 14) |