Skip to content

Commit

Permalink
Update future.en.md
Browse files Browse the repository at this point in the history
  • Loading branch information
c8ef authored Oct 28, 2024
1 parent 3d6fd44 commit fc32d89
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions docs/future.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,50 @@ using ::babylon::Future;
using ::babylon::Promise;

{
Promise<int> promise;
Promise<int> promise;
auto future = promise.get_future();
::std::thread thread([&] () {
::std::thread thread([&]() {
// Perform some asynchronous operations
...
// Set the final value
// Set the final value
promise.set_value(10086);
});
future.get(); // Waits for set_value, result == 10086
future.get(); // Waits for set_value, result == 10086
}

{
// Example using an XSchedInterface coroutine mechanism for synchronization
Promise<int, XSchedInterface> promise;
Promise<int, XSchedInterface> promise;
auto future = promise.get_future();
XThread thread([&] () {
XThread thread([&]() {
// Perform some asynchronous operations
...
// Set the final value
// Set the final value
promise.set_value(10086);
});
future.get(); // Waits for set_value (using XSchedInterface for coroutine synchronization without occupying pthread workers), result == 10086
future.get(); // Waits for set_value (using XSchedInterface for coroutine
// synchronization without occupying pthread workers), result
// == 10086
}

{
Promise<int> promise;
Promise<int> promise;
auto future = promise.get_future();
// Move-capture promise to avoid destruction out of scope
::std::thread thread([promise = ::std::move(promise)] () mutable {
// Move-capture promise to avoid destruction out of scope
::std::thread thread([promise = ::std::move(promise)]() mutable {
// Perform some asynchronous operations
...
// Set the final value
// Set the final value
promise.set_value(10086);
});
Promise<int> promise2;
auto future2 = promise2.get_future();
future.on_finish([promise2 = ::std::move(promise2)] (int&& value) {
// Called by set_value, with value == 10086
promise2.set_value(value + 10010);
});
// After on_finish, future is no longer available, cannot be ready or get
future2.get(); // Waits for set_value, result == 20096
auto future2 = promise2.get_future();
future.on_finish([promise2 = ::std::move(promise2)](int&& value) {
// Called by set_value, with value == 10086
promise2.set_value(value + 10010);
});
// After on_finish, future is no longer available, cannot be ready or get
future2.get(); // Waits for set_value, result == 20096
}

// For further details, see comments and test cases
Expand Down

0 comments on commit fc32d89

Please sign in to comment.