upload
This commit is contained in:
parent
05ecc0551e
commit
ea328ae95b
@ -39,7 +39,72 @@ modification date: 星期五 26日 一月 2024 09:55:11
|
||||
- IO多路复用,由调度器恢复
|
||||
|
||||
# 代码
|
||||
## coroutine
|
||||
|
||||
- Frame
|
||||
- promise_type promise
|
||||
- get_return_object()
|
||||
- initial_suspsend()
|
||||
- unhandled_exception()
|
||||
- final_suspsend()
|
||||
|
||||
```c++
|
||||
void coroutine(){
|
||||
frame = operator new(…);
|
||||
frame::promise_type& promise = frame->promise;
|
||||
// 在初次挂起时返回给调用者
|
||||
auto return_value = promise.get_return_object();
|
||||
co_await promise.initial_suspsend();
|
||||
try{
|
||||
//frame.func_body();
|
||||
func_body();
|
||||
//if no co_return; promise.return_void();
|
||||
}catch(...){
|
||||
promise.unhandled_exception();
|
||||
}
|
||||
co_await promise.final_suspsend();
|
||||
}
|
||||
```
|
||||
|
||||
## co_await
|
||||
|
||||
- awaiter
|
||||
- await_ready()
|
||||
- await_suspend(handle)
|
||||
- await_resume()
|
||||
|
||||
```c++
|
||||
void await(){
|
||||
awaiter&& __a;
|
||||
if (!__a.await_ready()) {
|
||||
__a.await_suspend(协程句柄);
|
||||
// 挂起/恢复点
|
||||
}
|
||||
auto result = __a.await_resume();
|
||||
}
|
||||
```
|
||||
|
||||
## co_yield
|
||||
|
||||
- promise.yield_value()
|
||||
|
||||
```
|
||||
co_yield 表达式
|
||||
co_await promise.yield_value(表达式);
|
||||
```
|
||||
|
||||
# 同步
|
||||
|
||||
## 锁
|
||||
|
||||
```c++
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
|
||||
## 条件变量
|
||||
|
||||
## 信号量
|
||||
|
||||
## 信号通道
|
||||
|
||||
|
||||
151
src/c/c++/value type.md
Normal file
151
src/c/c++/value type.md
Normal file
@ -0,0 +1,151 @@
|
||||
---
|
||||
aliases:
|
||||
tags:
|
||||
creation date: 2024-02-02 13:17
|
||||
modification date: 星期五 2日 二月 2024 13:17:0
|
||||
---
|
||||
# 值类型
|
||||
|
||||
- 等号左边的是左值,右边的是右值? x
|
||||
- 左值和右值可以相互转换
|
||||
- ++x 与 x++,返回的值类型不同
|
||||
|
||||
## 左值
|
||||
|
||||
- 能取地址的是左值,能修改
|
||||
- 左值有相对稳定的内存地址,生命周期长
|
||||
|
||||
## 右值
|
||||
|
||||
- 不能取地址的是右值,不能修改
|
||||
- 右值是不指向稳定地址的匿名对象,生命周期短
|
||||
|
||||
## 将亡值
|
||||
|
||||
- 属于左值又属于右值
|
||||
- 储存中间结果的变量
|
||||
- 只要知道地址就可以修改
|
||||
|
||||
## 特性
|
||||
|
||||
- 右值引用是左值可修改的,持有的是右值的拷贝地址
|
||||
|
||||
# 函数
|
||||
|
||||
## 函数参数
|
||||
|
||||
- T t
|
||||
- 值传递,触发对象拷贝
|
||||
- T& t
|
||||
- 传递引用,运行时逻辑等同指针
|
||||
- 编译时可以避免指针判空
|
||||
- 只能传递左值,不能传右值
|
||||
- const T& t
|
||||
- 传递引用,只能传常左值,或右值
|
||||
- 常左值能传不能改
|
||||
- T&& t
|
||||
- 左值、右值都能传递
|
||||
- 延长右值生命周期,减少对象复制
|
||||
- 编译器会优先使用移动构造函数生成临时对象
|
||||
- 移动构造 所有权转移
|
||||
|
||||
## 函数返回值
|
||||
|
||||
- 返回值是中间类型
|
||||
- 可能会存在两次类型转换
|
||||
- 需要进行返回值优化
|
||||
|
||||
# 实用
|
||||
|
||||
## 万能引用
|
||||
|
||||
模板函数 | auto 可以实现万能引用
|
||||
|
||||
左值参数推导为左值引用
|
||||
|
||||
右值参数推导为右值引用
|
||||
|
||||
## 完美转发
|
||||
|
||||
万能引用可以实现完美转发
|
||||
|
||||
|
||||
|
||||
# 类型转换
|
||||
|
||||
当类型不一致时,编辑器会调用隐式转换
|
||||
|
||||
如果目标对象是复合对象,如class、struct
|
||||
|
||||
会调用对应的构造函数,调用原则参照函数重载
|
||||
|
||||
## 默认构造
|
||||
|
||||
## 拷贝构造
|
||||
|
||||
## 移动构造
|
||||
|
||||
```
|
||||
#include <condition_variable>
|
||||
#include <semaphore>
|
||||
#include <iostream>
|
||||
namespace zstd {
|
||||
template<size_t T>
|
||||
struct static_buffer {
|
||||
char data[T];
|
||||
};
|
||||
template<std::move_constructible T, size_t T_capacity = 1>
|
||||
class channel {
|
||||
static_assert(T_capacity != 0);
|
||||
protected:
|
||||
int m_tail;
|
||||
int m_head;
|
||||
int m_size;
|
||||
static_buffer<sizeof(T)> m_buf[T_capacity];
|
||||
std::mutex m_mtx;
|
||||
std::condition_variable m_cv;
|
||||
public:
|
||||
~channel() {
|
||||
reset();
|
||||
}
|
||||
channel() : m_tail(0), m_head(0), m_buf(), m_size(T_capacity){}
|
||||
int head() {
|
||||
return m_head;
|
||||
}
|
||||
int tail() {
|
||||
return m_tail;
|
||||
}
|
||||
int count() {
|
||||
return m_head - m_tail;
|
||||
}
|
||||
void reset() {
|
||||
for (int i = m_tail; i < m_head; i++) {
|
||||
std::destroy_at((T*)(m_buf + m_tail % m_size));
|
||||
}
|
||||
m_tail = 0;
|
||||
m_head = 0;
|
||||
}
|
||||
template<typename... Args>
|
||||
void release(Args... args) {
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_head >= m_tail + m_size) {
|
||||
m_cv.wait(lck);
|
||||
}
|
||||
std::construct_at((T*)(m_buf + m_head % m_size), std::forward<Args>(args)...);
|
||||
if(m_head++ == m_tail)
|
||||
m_cv.notify_one();
|
||||
};
|
||||
T acquire() {//不能传右值引用,右值引用拷贝地址数据不在锁保护范围内
|
||||
std::unique_lock<std::mutex> lck(m_mtx);
|
||||
while (m_tail == m_head) {
|
||||
m_cv.wait(lck);
|
||||
}
|
||||
int tail = m_tail % m_size;
|
||||
if(m_tail++ == m_head - m_size)
|
||||
m_cv.notify_one();
|
||||
return std::move(*(T*)(m_buf + tail));
|
||||
};
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Loading…
Reference in New Issue
Block a user