Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Apr 20, 2024
1 parent d1b1306 commit 9ab1129
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
4 changes: 2 additions & 2 deletions doc/docs/api/vcpu-and-multicore.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
34 changes: 19 additions & 15 deletions doc/docs/introduction/write-first-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
};
Expand Down Expand Up @@ -92,17 +92,21 @@ bool condition = false;
photon_std::mutex mu;
photon_std::condition_variable cv;

// Producer thread
photon_std::lock_guard<photon_std::mutex> lock(mu);
condition = true;
cv.notify_one();

// Consumer thread
auto timeout = std::chrono::duration<std::chrono::seconds>(10);
photon_std::unique_lock<photon_std::mutex> lock(mu);
while (!condition) {
cv.wait(lock, timeout);
}
photon_std::thread([&]{
auto timeout = std::chrono::duration<std::chrono::seconds>(10);
photon_std::unique_lock<photon_std::mutex> lock(mu);
while (!condition) {
cv.wait(lock, timeout);
}
}).detach();

// Producer thread
photon_std::thread([&]{
photon_std::lock_guard<photon_std::mutex> lock(mu);
condition = true;
cv.notify_one();
}).detach();
```
### 5. File IO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
};
Expand Down Expand Up @@ -87,17 +87,21 @@ bool condition = false;
photon_std::mutex mu;
photon_std::condition_variable cv;

// 生产者协程
photon_std::lock_guard<photon_std::mutex> lock(mu);
condition = true;
cv.notify_one();

// 消费者协程
auto timeout = std::chrono::duration<std::chrono::seconds>(10);
photon_std::unique_lock<photon_std::mutex> lock(mu);
while (!condition) {
cv.wait(lock, timeout);
}
photon_std::thread([&]{
auto timeout = std::chrono::duration<std::chrono::seconds>(10);
photon_std::unique_lock<photon_std::mutex> lock(mu);
while (!condition) {
cv.wait(lock, timeout);
}
}).detach();

// 生产者协程
photon_std::thread([&]{
photon_std::lock_guard<photon_std::mutex> lock(mu);
condition = true;
cv.notify_one();
}).detach();
```
### 5. 文件 IO
Expand Down

0 comments on commit 9ab1129

Please sign in to comment.