doc: update cpp.md (#254)
This commit is contained in:
parent
3f84fe66d3
commit
b08cc85e41
23
docs/cpp.md
23
docs/cpp.md
@ -628,18 +628,19 @@ std::for_each(vec.begin(), vec.end(), [](int& ele) -> void
|
|||||||
|
|
||||||
## C++多线程
|
## C++多线程
|
||||||
|
|
||||||
> g++编译选项:`std=c++11`
|
### 多线程 介绍
|
||||||
>
|
|
||||||
> 包含头文件:
|
g++编译选项:`std=c++11`,包含头文件:
|
||||||
>
|
|
||||||
> + `#include <thread>`:C++多线程库
|
- `#include <thread>`:C++多线程库
|
||||||
> + `#include <mutex>`:C++互斥量库
|
- `#include <mutex>`:C++互斥量库
|
||||||
|
|
||||||
### 线程的创建
|
### 线程的创建
|
||||||
|
<!--rehype:wrap-class=row-span-2-->
|
||||||
|
|
||||||
以普通函数作为线程入口函数:
|
以普通函数作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
void thread_entry_function_1() { }
|
void thread_entry_function_1() { }
|
||||||
void thread_entry_function_2(int val) { }
|
void thread_entry_function_2(int val) { }
|
||||||
|
|
||||||
@ -649,7 +650,7 @@ std::thread my_thread_2(thread_entry_function_2, 5);
|
|||||||
|
|
||||||
以类对象作为线程入口函数:
|
以类对象作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
class Entry
|
class Entry
|
||||||
{
|
{
|
||||||
void operator()() { }
|
void operator()() { }
|
||||||
@ -665,7 +666,7 @@ std::thread my_thread_2(&Entry::entry_function, &entry);
|
|||||||
|
|
||||||
以lambda表达式作为线程入口函数:
|
以lambda表达式作为线程入口函数:
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::thread my_thread([]() -> void
|
std::thread my_thread([]() -> void
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
@ -674,7 +675,7 @@ std::thread my_thread([]() -> void
|
|||||||
|
|
||||||
### 线程的销毁
|
### 线程的销毁
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
thread my_thread;
|
thread my_thread;
|
||||||
// 阻塞
|
// 阻塞
|
||||||
my_thread.join();
|
my_thread.join();
|
||||||
@ -684,7 +685,7 @@ my_thread.detach();
|
|||||||
|
|
||||||
### `this_thread`
|
### `this_thread`
|
||||||
|
|
||||||
```c++
|
```cpp
|
||||||
std::this_thread::get_id(); // 获取当前线程ID
|
std::this_thread::get_id(); // 获取当前线程ID
|
||||||
std::this_thread::sleep_for(); // 使当前线程休眠一段指定时间
|
std::this_thread::sleep_for(); // 使当前线程休眠一段指定时间
|
||||||
std::this_thread::sleep_until();// 使当前线程休眠到指定时间
|
std::this_thread::sleep_until();// 使当前线程休眠到指定时间
|
||||||
|
Loading…
x
Reference in New Issue
Block a user