From 91ee61fac08e1ea2561c289db8edc3813d3842bb Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 28 Oct 2024 22:59:08 +0800 Subject: [PATCH 1/4] Update future.zh-cn.md --- docs/future.zh-cn.md | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/future.zh-cn.md b/docs/future.zh-cn.md index 018e3d6..627602e 100644 --- a/docs/future.zh-cn.md +++ b/docs/future.zh-cn.md @@ -21,48 +21,48 @@ using ::babylon::Future; using ::babylon::Promise; { - Promise promise; + Promise promise; auto future = promise.get_future(); - ::std::thread thread([&] () { + ::std::thread thread([&]() { // 异步做一些事情 ... - // 最终赋值 + // 最终赋值 promise.set_value(10086); }); - future.get(); // 等待set_value,结果 == 10086 + future.get(); // 等待set_value,结果 == 10086 } - + { // 例如有一种XSchedInterface的协程机制,使用对应的机制来同步 - Promise promise; + Promise promise; auto future = promise.get_future(); - XThread thread([&] () { + XThread thread([&]() { // 异步做一些事情 ... - // 最终赋值 + // 最终赋值 promise.set_value(10086); }); - future.get(); // 等待set_value(使用XSchedInterface协程同步,不占用pthread worker),结果 == 10086 + future.get(); // 等待set_value(使用XSchedInterface协程同步,不占用pthread worker),结果 == 10086 } - + { - Promise promise; + Promise promise; auto future = promise.get_future(); - // 移动捕获promise,避免出作用域销毁 - ::std::thread thread([promise = ::std::move(promise)] () mutable { + // 移动捕获promise,避免出作用域销毁 + ::std::thread thread([promise = ::std::move(promise)]() mutable { // 异步做一些事情 ... - // 最终赋值 + // 最终赋值 promise.set_value(10086); }); Promise promise2; - auto future2 = promise2.get_future(); - future.on_finish([promise2 = ::std::move(promise2)] (int&& value) { - // 由set_value调用,传入value == 10086 - promise2.set_value(value + 10010); - }); - // on_finish之后future不再可用,无法ready或者get - future2.get(); // 等待set_value,结果 == 20096 + auto future2 = promise2.get_future(); + future.on_finish([promise2 = ::std::move(promise2)](int&& value) { + // 由set_value调用,传入value == 10086 + promise2.set_value(value + 10010); + }); + // on_finish之后future不再可用,无法ready或者get + future2.get(); // 等待set_value,结果 == 20096 } // 更说明见注释 From 3d6fd44c5764a94ce266bde398ff02359ba360b8 Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 28 Oct 2024 23:00:37 +0800 Subject: [PATCH 2/4] Update future.zh-cn.md --- docs/future.zh-cn.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/future.zh-cn.md b/docs/future.zh-cn.md index 627602e..ef307c8 100644 --- a/docs/future.zh-cn.md +++ b/docs/future.zh-cn.md @@ -26,8 +26,8 @@ using ::babylon::Promise; ::std::thread thread([&]() { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); future.get(); // 等待set_value,结果 == 10086 } @@ -39,10 +39,11 @@ using ::babylon::Promise; XThread thread([&]() { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); - future.get(); // 等待set_value(使用XSchedInterface协程同步,不占用pthread worker),结果 == 10086 + future.get(); // 等待set_value(使用XSchedInterface协程同步,不占用pthread + // worker),结果 == 10086 } { @@ -52,8 +53,8 @@ using ::babylon::Promise; ::std::thread thread([promise = ::std::move(promise)]() mutable { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); Promise promise2; auto future2 = promise2.get_future(); From fc32d895e5a9634c2dad5bded6db6b9a53c8e81b Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 28 Oct 2024 23:01:21 +0800 Subject: [PATCH 3/4] Update future.en.md --- docs/future.en.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/docs/future.en.md b/docs/future.en.md index 289b605..ad08219 100644 --- a/docs/future.en.md +++ b/docs/future.en.md @@ -20,48 +20,50 @@ using ::babylon::Future; using ::babylon::Promise; { - Promise promise; + Promise 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 promise; + Promise 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 promise; + Promise 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 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 From bec3e7e85e19ec6fd74af50c75614daae2f7f65c Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 28 Oct 2024 23:02:55 +0800 Subject: [PATCH 4/4] Update future.zh-cn.md --- docs/future.zh-cn.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/future.zh-cn.md b/docs/future.zh-cn.md index ef307c8..1f8f196 100644 --- a/docs/future.zh-cn.md +++ b/docs/future.zh-cn.md @@ -26,8 +26,8 @@ using ::babylon::Promise; ::std::thread thread([&]() { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); future.get(); // 等待set_value,结果 == 10086 } @@ -39,8 +39,8 @@ using ::babylon::Promise; XThread thread([&]() { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); future.get(); // 等待set_value(使用XSchedInterface协程同步,不占用pthread // worker),结果 == 10086 @@ -53,8 +53,8 @@ using ::babylon::Promise; ::std::thread thread([promise = ::std::move(promise)]() mutable { // 异步做一些事情 ... - // 最终赋值 - promise.set_value(10086); + // 最终赋值 + promise.set_value(10086); }); Promise promise2; auto future2 = promise2.get_future();