Implementation of a promise/future system for coroutines.
This file defines the promise and future types used to manage coroutine execution. It provides mechanisms to retrieve coroutine results (or exceptions) and to coordinate multiple asynchronous operations.
Example Usage
#include <vector>
#include <iostream>
using namespace NNet;
co_return 42;
}
co_return;
}
std::vector<TFuture<int>> futures;
futures.push_back(getInt());
futures.push_back(getInt());
auto results =
co_await All(std::move(futures));
for (auto res : results) {
std::cout << "Result: " << res << std::endl;
}
co_return;
}
std::vector<TFuture<int>> futures;
futures.push_back(getInt());
futures.push_back(getInt());
int result =
co_await Any(std::move(futures));
co_return result;
}
Implementation of a promise/future system for coroutines.
TFuture< T > Any(std::vector< TFuture< T > > &&futures)
Returns the result of whichever future completes first.
Definition corochain.hpp:353
TFuture< std::vector< T > > All(std::vector< TFuture< T > > &&futures)
Awaits every future in order and collects their results.
Definition corochain.hpp:317
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185
template<typename T >
| TFuture< T > NNet::Any |
( |
std::vector< TFuture< T > > && |
futures | ) |
|
Returns the result of whichever future completes first.
If one future is already done, its result is returned without suspension. Otherwise, all futures register this coroutine as their continuation; when the first one resumes it, the remaining futures are abandoned (their frames are destroyed when the vector goes out of scope).
- Template Parameters
-
| T | The type of each coroutine's result. |
- Parameters
-
| futures | A vector of TFuture<T> objects (moved in). |
- Returns
- TFuture<T> with the result from the first completed future.