forked from yyzybb537/libgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample6_mutex.cpp
More file actions
74 lines (62 loc) · 1.95 KB
/
Copy pathsample6_mutex.cpp
File metadata and controls
74 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/************************************************
* libgo sample6
************************************************
* 在多线程编程中,你经常会使用到一些锁用来同步代码。
* libgo目前还没有支持OS原生的锁的自动co_yield,
* 不过libgo提供了一个协程锁co_mutex,可以达到
* 同样的效果,并且在等待协程锁期间可以让出cpu,执行
* 其他协程。
*
* co_mutex提供了与标准库的mutex相同的操作接口,可
* 以轻松搭配标准库提供的lock_guard uniqe_lock等工
* 具。
*
* 和标准库的mutex一样,使用时要注意生命期的控制,
* 不要在已经析构的co_mutex上进行操作
************************************************/
#include <mutex>
#include "coroutine.h"
#include "win_exit.h"
int main()
{
co_mutex cm;
// 为展示co_mutex自动切换协程的功能,先锁住mutex
cm.lock();
go [&]{
for (int i = 0; i < 3; ++i) {
std::lock_guard<co_mutex> lock(cm);
printf("coroutine 1 lock\n");
}
};
go [&]{
cm.unlock();
for (int i = 0; i < 3; ++i) {
std::lock_guard<co_mutex> lock(cm);
printf("coroutine 2 lock\n");
}
};
// 读写锁
co_rwmutex m;
go [&]{
{
// 读锁
m.reader().lock();
m.reader().unlock();
// 或使用标准库提供的lock系列的工具
// 读锁视图的类型为:co_rmutex
std::unique_lock<co_rmutex> lock(m.Reader());
}
{
// 写锁
m.writer().lock();
m.writer().unlock();
// 或使用标准库提供的lock系列的工具
// 写锁视图的类型为:co_wmutex
std::unique_lock<co_wmutex> lock(m.Writer());
}
};
// 200ms后安全退出
std::thread([]{ co_sleep(200); co_sched.Stop(); }).detach();
co_sched.Start();
return 0;
}