On this page
article
Share
Share ensures that a single underlying execution is shared among all concurrent callers, even with different arguments. It is similar to deduplicate but typically shares the result of a no-argument or global operation.
Share
What it is
Share ensures that a single underlying execution is shared among all concurrent callers, even with different arguments. It is similar to deduplicate but typically shares the result of a no-argument or global operation.
When to use it
- One-time initialization that multiple callers await.
- Singleton setup that must not run twice.
- Shared resource warm-up.
Async / sync support
| Wrapper | Support |
|---|---|
Func<R> | ✅ Async |
Func1<T, R> | ✅ Async |
Func2<T1, T2, R> | ✅ Async |
FuncSync<R> | ❌ No |
API reference
| |
No parameters.
Examples
Basic example
| |
Real-world example
| |
Best practices
- Use
sharefor idempotent, expensive setup. - Do not use
sharefor operations that should run per caller. - Combine with
onceif the operation should run at most once for the lifetime of the wrapper.
Common pitfalls
- Retried failures are not shared: If the shared future fails, subsequent callers may trigger a new attempt depending on implementation.
- Arguments ignored: For
Func1/Func2, shared execution means later callers’ arguments may be ignored.