From 9ab1129979b393d53d51277c75f34a61d0f84a66 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Sat, 20 Apr 2024 18:51:15 +0800 Subject: [PATCH] update doc --- doc/docs/api/vcpu-and-multicore.md | 4 +-- doc/docs/introduction/write-first-example.md | 34 ++++++++++-------- .../current/api/vcpu-and-multicore.md | 4 +-- .../introduction/write-first-example.md | 36 ++++++++++--------- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/doc/docs/api/vcpu-and-multicore.md b/doc/docs/api/vcpu-and-multicore.md index e493a671..254d4bcb 100644 --- a/doc/docs/api/vcpu-and-multicore.md +++ b/doc/docs/api/vcpu-and-multicore.md @@ -20,13 +20,13 @@ Currently, there are only two ways in Photon to utilize multiple cores: `thread_migrate` could be used to migrate thread to other vCPU. ```cpp -new std::thread([&]{ +std::thread([]{ photon::init(); DEFER(photon::fini()); auto th = photon::thread_create11(func); photon::thread_migrate(th, vcpu); -}); +}).detach(); ``` ### 2. Use `WorkPool` diff --git a/doc/docs/introduction/write-first-example.md b/doc/docs/introduction/write-first-example.md index fd1090c5..1231822f 100644 --- a/doc/docs/introduction/write-first-example.md +++ b/doc/docs/introduction/write-first-example.md @@ -50,19 +50,19 @@ There are many ways to create a thread. Just like the old ways you use `std::thr int func(int a, char* b) {} // Use global function to create a thread. -// Will be automatically joined when thread object destructed. +// Will be automatically joined when thread object destructed, unless been detached. photon_std::thread th(func, 1, '2'); +th.detach(); // Create a thread with anonymous function (lambda) photon_std::thread th([&] { - // Access local variables directly without passing arguments - } -); + // Access variables directly in the context +}); // Create a thread with class member function class A { void f() { - auto th = new photon_std::thread(&A::g, this, 1, '2'); + new photon_std::thread(&A::g, this, 1, '2'); } void g(int a, char* b) {} }; @@ -92,17 +92,21 @@ bool condition = false; photon_std::mutex mu; photon_std::condition_variable cv; -// Producer thread -photon_std::lock_guard lock(mu); -condition = true; -cv.notify_one(); - // Consumer thread -auto timeout = std::chrono::duration(10); -photon_std::unique_lock lock(mu); -while (!condition) { - cv.wait(lock, timeout); -} +photon_std::thread([&]{ + auto timeout = std::chrono::duration(10); + photon_std::unique_lock lock(mu); + while (!condition) { + cv.wait(lock, timeout); + } +}).detach(); + +// Producer thread +photon_std::thread([&]{ + photon_std::lock_guard lock(mu); + condition = true; + cv.notify_one(); +}).detach(); ``` ### 5. File IO diff --git a/doc/i18n/cn/docusaurus-plugin-content-docs/current/api/vcpu-and-multicore.md b/doc/i18n/cn/docusaurus-plugin-content-docs/current/api/vcpu-and-multicore.md index 1dc51490..51fcad26 100644 --- a/doc/i18n/cn/docusaurus-plugin-content-docs/current/api/vcpu-and-multicore.md +++ b/doc/i18n/cn/docusaurus-plugin-content-docs/current/api/vcpu-and-multicore.md @@ -20,13 +20,13 @@ Photon 的 vCPU 的概念等价于 OS 线程。 可以利用`thread_migrate`将协程迁移到其他vCPU上去 ```cpp -new std::thread([&]{ +std::thread([&]{ photon::init(); DEFER(photon::fini()); auto th = photon::thread_create11(func); photon::thread_migrate(th, vcpu); -}); +}).detach(); ``` ### 2. 使用 `WorkPool` diff --git a/doc/i18n/cn/docusaurus-plugin-content-docs/current/introduction/write-first-example.md b/doc/i18n/cn/docusaurus-plugin-content-docs/current/introduction/write-first-example.md index 88822fb7..20fa295f 100644 --- a/doc/i18n/cn/docusaurus-plugin-content-docs/current/introduction/write-first-example.md +++ b/doc/i18n/cn/docusaurus-plugin-content-docs/current/introduction/write-first-example.md @@ -46,19 +46,19 @@ int main() { // 全局函数 int func(int a, char* b) {} -// 用全局函数创建协程,thread对象析构时自动Join +// 用全局函数创建协程,thread对象析构时自动Join,除非调用了detach photon_std::thread th(func, 1, '2'); +th.detach(); // 或者使用匿名函数(lambda) -new photon_std::thread([&] { - // 不用传参,可以直接访问局部变量 - } -); +photon_std::thread th([&] { + // 直接访问上下文中的变量 +}); // 用类的成员函数创建协程 class A { void f() { - auto th = new photon_std::thread(&A::g, this, 1, '2'); + new photon_std::thread(&A::g, this, 1, '2'); } void g(int a, char* b) {} }; @@ -87,17 +87,21 @@ bool condition = false; photon_std::mutex mu; photon_std::condition_variable cv; -// 生产者协程 -photon_std::lock_guard lock(mu); -condition = true; -cv.notify_one(); - // 消费者协程 -auto timeout = std::chrono::duration(10); -photon_std::unique_lock lock(mu); -while (!condition) { - cv.wait(lock, timeout); -} +photon_std::thread([&]{ + auto timeout = std::chrono::duration(10); + photon_std::unique_lock lock(mu); + while (!condition) { + cv.wait(lock, timeout); + } +}).detach(); + +// 生产者协程 +photon_std::thread([&]{ + photon_std::lock_guard lock(mu); + condition = true; + cv.notify_one(); +}).detach(); ``` ### 5. 文件 IO