Skip to content

Commit 75ca9a0

Browse files
committed
Create spin_lock
1 parent 71228b9 commit 75ca9a0

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
1717
| [encrypt_decrypt](https://github.com/AimTao/tool-by-cpp/tree/master/executable/encrypt_decrypt) | [命令行式](https://github.com/AimTao/tool-by-cpp/releases/tag/v2.0.0)[交互式](https://github.com/AimTao/tool-by-cpp/releases/tag/v2.1.0) | 对任何文件可进行加密解密(随机分组加密)。 |
1818
| [encrypt_decrypt_double](https://github.com/AimTao/tool-by-cpp/tree/master/executable/encrypt_decrypt_double) | [命令行式](https://github.com/AimTao/tool-by-cpp/releases/tag/v3.0.0)[交互式](https://github.com/AimTao/tool-by-cpp/releases/tag/v3.1.0) | [encrypt_decrypt](https://github.com/AimTao/tool-by-cpp/tree/master/executable/encrypt_decrypt)的升级版,添加二重加密。 |
19-
| [mutex](https://github.com/AimTao/tool-by-cpp/tree/master/executable/mutex) | [命令行式](https://github.com/AimTao/tool-by-cpp/releases/tag/v4.0.0) | 验证生产者与消费者问题的工具。 |
20-
| | | |
19+
| [mutex](https://github.com/AimTao/tool-by-cpp/tree/master/executable/mutex) | [命令行式](https://github.com/AimTao/tool-by-cpp/releases/tag/v4.0.0) | 验证生产者与消费者问题中互斥锁的工具。 |
20+
| [spin_lock](https://github.com/AimTao/tool-by-cpp/tree/master/executable/spin_lock) | [命令行式](https://github.com/AimTao/tool-by-cpp/releases/tag/v5.0.0) | 验证生产者与消费者问题中自旋锁的工具。 |
2121

2222

2323

executable/spin_lock/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## spin_lock
2+
3+
一个验证生产者与消费者问题中的自旋锁的程序。
4+
5+
6+
7+
## 编译
8+
9+
> Windows 自行编译运行,Linux的二进制文件可在 [releases](https://github.com/AimTao/tool-by-cpp/releases/tag/v5.0.0) 中下载。
10+
11+
⚠️注意:
12+
13+
+ 由于 macOS 不支持**POSIX标准提供的编程接口**,所以不能编译和运行。
14+
15+
+ 由于 pthread 库不是 Linux 系统默认的库,连接时需要使用库 `libpthread.a`,所以在使用 `pthread_create` 创建线程时,在编译中要加 `-lpthread` 参数:`g++ spin_lock.cc -o spin_ -lpthread `
16+
17+
18+
19+
## 使用
20+
21+
`./spin_lock` 执行程序后,会弹出一下提示:
22+
23+
```
24+
================= option =================
25+
Add spin_lock, please input 1
26+
Don't add spin_lock, please input 0
27+
==========================================
28+
```
29+
30+
+ 想验证 *生产者消费者问题* 请输入 0。
31+
+ 想验证 *加入自旋锁后的生产者消费者问题* 请输入 1。
32+
33+
34+
35+
## 验证结果
36+
37+
+ 未加入自旋锁时,nums 不为 0;
38+
39+
+ 加入自旋锁时,nums 为 0。

source/spin_lock/spin_lock.cc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <pthread.h>
5+
6+
pthread_spinlock_t spin_lock;
7+
8+
int key = 0;
9+
10+
// 临界资源
11+
int nums = 0;
12+
13+
// 生产者
14+
void *producer(void*) {
15+
int times = 10000000;
16+
if (key == 1) {
17+
while(times--) {
18+
pthread_spin_lock(&spin_lock);
19+
nums ++;
20+
pthread_spin_unlock(&spin_lock);
21+
}
22+
} else if (key == 0) {
23+
while(times--) {
24+
nums ++;
25+
}
26+
}
27+
}
28+
29+
// 消费者
30+
void *comsumer(void*) {
31+
int times = 10000000;
32+
if (key == 1) {
33+
while(times--) {
34+
pthread_spin_lock(&spin_lock);
35+
nums --;
36+
pthread_spin_unlock(&spin_lock);
37+
}
38+
} else if (key == 0) {
39+
while(times--) {
40+
nums --;
41+
}
42+
}
43+
}
44+
45+
int main() {
46+
printf("================= option =================\n");
47+
printf("Add spin_lock, please input 1\n");
48+
printf("Don't add spin_lock, please input 0\n");
49+
printf("==========================================\n\n");
50+
printf("Please input your option: ");
51+
scanf("%d", &key);
52+
if (key != 0 && key != 1) {
53+
printf("Input error, please input 1 or 0\n");
54+
return 0;
55+
}
56+
pthread_spin_init(&spin_lock, 0);
57+
printf("Start in main function: num.\n");
58+
pthread_t thread1, thread2;
59+
pthread_create( &thread1, NULL, &producer, NULL );
60+
pthread_create( &thread2, NULL, &comsumer, NULL );
61+
pthread_join(thread1, NULL);
62+
pthread_join(thread2, NULL);
63+
printf("Print in main function: num = %d", nums);
64+
return 0;
65+
}

0 commit comments

Comments
 (0)