补充笔记、线程示例

This commit is contained in:
2024-07-24 10:06:29 +08:00
parent 9d57483c76
commit a66c2910bd
19 changed files with 227 additions and 28 deletions

72
Day3/README.md Normal file
View File

@@ -0,0 +1,72 @@
# Day 3 IPC
![alt text](image-3.png)
![alt text](image.png)
1.
2.
3.
## 临界区
only one can use the resource at a time
有人用了,别人就不能用
## 阻塞非阻塞
Blocking/Non-blocking
![alt text](image-1.png)
阻塞: 等待,一个线程
非阻塞: 不等待
## 挂起
<!-- ## 同步
一个线程在等待另一个线程
## 异步
一个线程在等待另一个线程,另一个线程在等待另一个线程 -->
## 死锁
两个线程互相等待
![alt text](image-2.png)
<!-- ## 互斥
两个线程不能同时使用资源 -->
## 信号量
用于线程间同步、互斥
- 二值信号量 约等于bool 获得1不得0
![alt text](image-4.png)
- 计数信号量 初始为期待的值???
- ![alt text](image-5.png)
- 裸机 根据全局变量flag 反应(错误、破坏、不能挂起一直停在这……???)
- 用系统的api不要flag
- 三种反应:一直等,等一会,不等
## api
### √ 创建信号量(动态)节省资源,动态分配,可能内存破坏
注意区别?
``` c
rt_sem_t rt_sem_create(const char* name, rt_uint32_t value, rt_uint8_t flag);
// flag:RT_IPC_FLAG_FIFO先进先出/RT_IPC_FLAG_PRIO优先级
```
### √ 删除信号量
``` c
rt_err_t rt_sem_delete(rt_sem_t sem);
```
### 初始化信号量(静态)还在内存,别人也用不了
``` c
rt_err_t rt_sem_init(rt_sem_t sem, const char* name, rt_uint32_t value, rt_uint8_t flag);
```
### 脱离信号量
从内核对象管理器中脱离
### 获取信号量
time 单位tick
![alt text](image-6.png)
### 无等待获取信号量
``` c
rt_err_t rt_sem_trytake(rt_sem_t sem);
```
### 释放信号量
``` c
rt_err_t rt_sem_release(rt_sem_t sem);
```

BIN
Day3/image-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 KiB

BIN
Day3/image-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

BIN
Day3/image-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

BIN
Day3/image-4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

BIN
Day3/image-5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

BIN
Day3/image-6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB

BIN
Day3/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB