@@ -6,7 +6,7 @@ These include:
66* Coroutine Types
77 * ` task<T> `
88 * ` lazy_task<T> `
9- * ` shared_task<T> ` (coming - lewissbaker/cppcoro # 2 )
9+ * ` shared_task<T> `
1010 * ` shared_lazy_task<T> ` (coming - lewissbaker/cppcoro #2 )
1111 * ` generator<T> ` (coming - lewissbaker/cppcoro #5 )
1212 * ` recursive_generator<T> ` (coming - lewissbaker/cppcoro #6 )
@@ -223,7 +223,87 @@ the coroutine function and unwinds the stack back to the caller before
223223it can be awaited. The awaiting coroutine will continue execution without
224224suspending if the coroutine completed synchronously.
225225
226- ## single_consumer_event
226+ ## ` shared_task<T> `
227+
228+ The ` shared_task<T> ` class is a coroutine type that yields a single value
229+ asynchronously.
230+
231+ API Summary
232+ ``` c++
233+ namespace cppcoro
234+ {
235+ template<typename T = void >
236+ class shared_task
237+ {
238+ public:
239+ shared_task() noexcept;
240+ shared_task(const shared_task& other) noexcept;
241+ shared_task(shared_task&& other) noexcept;
242+ shared_task& operator=(const shared_task& other) noexcept;
243+ shared_task& operator=(shared_task&& other) noexcept;
244+
245+ void swap(shared_task& other) noexcept;
246+
247+ // Query if the task has completed and the result is ready.
248+ bool is_ready() const noexcept;
249+
250+ // Returns an operation that when awaited will suspend the
251+ // current coroutine until the task completes and the result
252+ // is available.
253+ //
254+ // The type of the result of the 'co_await someTask' expression
255+ // is an l-value reference to the task's result value (unless T
256+ // is void in which case the expression has type 'void').
257+ // If the task completed with an unhandled exception then the
258+ // exception will be rethrown by the co_await expression.
259+ <unspecified> operator co_await() const noexcept;
260+
261+ // Returns an operation that when awaited will suspend the
262+ // calling coroutine until the task completes and the result
263+ // is available.
264+ //
265+ // The result is not returned from the co_await expression.
266+ // This can be used to synchronise with the task without the
267+ // possibility of the co_await expression throwing an exception.
268+ <unspecified> when_ready() const noexcept;
269+
270+ };
271+
272+ template<typename T >
273+ bool operator==(const shared_task<T >& a, const shared_task<T >& b) noexcept;
274+ template<typename T >
275+ bool operator!=(const shared_task<T >& a, const shared_task<T >& b) noexcept;
276+
277+ template<typename T >
278+ void swap(shared_task<T >& a, shared_task<T >& b) noexcept;
279+
280+ // Wrap a task in a shared_task to allow multiple coroutines to concurrently
281+ // await the result.
282+ template<typename T >
283+ shared_task<T > make_shared_task(task<T > task);
284+ }
285+ ```
286+
287+ All const-methods on ` shared_task<T> ` are safe to call concurrently with other const-methods on the same instance from multiple threads.
288+ It is not safe to call non-const methods of ` shared_task<T> ` concurrently with any other method on the same instance of a ` shared_task<T> ` .
289+
290+ ### Comparison to ` task<T> `
291+
292+ The ` shared_task<T> ` class is similar to ` task<T> ` in that the task starts execution
293+ immediately upon the coroutine function being called.
294+
295+ It differs from ` task<T> ` in that the resulting task object can
296+ be copied, allowing multiple task objects to reference the same
297+ asynchronous result. It also supports multiple coroutines concurrently
298+ awaiting the result of the task.
299+
300+ The trade-off is that the result is always an l-value reference to the
301+ result, never an r-value reference (since the result may be shared) which
302+ may limit ability to move-construct the result into a local variable.
303+ It also has a slightly higher run-time cost due to the need to maintain
304+ a reference count and support multiple awaiters.
305+
306+ ## ` single_consumer_event `
227307
228308This is a simple manual-reset event type that supports only a single
229309coroutine awaiting it at a time.
0 commit comments