File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ backgroundColor: white
3333- 早期的操作系统的主要同步机制
3434![ w:700] ( figs/basic-syncmutex.png )
3535
36+ <!--
37+ Dijkstra 应译作“戴克斯特拉”
38+ https://zhuanlan.zhihu.com/p/73390180
39+ 维基百科 Edsger Wybe Dijkstra 页面(https://en.wikipedia.org/wiki/Edsger_W._Dijkstra),Dijkstra 可读作 /ˈdaɪkstrə/ 或 [ˈdɛikstra]
40+ -->
3641---
3742
3843### 信号量(semaphore)
@@ -56,16 +61,128 @@ backgroundColor: white
5661 - 线程不会被无限期阻塞在P()操作
5762 - 假定信号量等待按先进先出排队
5863
59- 自旋锁能否实现先进先出?
64+ ** 问题 ** : 自旋锁能否实现先进先出?
6065
6166![ bg right:30% 100%] ( figs/sema-train.png )
6267
63-
6468---
6569
6670### 信号量在概念上的实现
71+
72+ <!--
6773
74+ -->
75+
76+ <font size =6 >
77+
78+ ```
79+ Class Semaphore {
80+ int sem;
81+ WaitQueue q;
82+ }
83+ ```
84+
85+ <style >
86+ .container {
87+ display : flex ;
88+ }
89+ .col {
90+ flex : 1 ;
91+ }
92+ </style >
93+
94+ <div class =" container " >
95+
96+ <div class =" col " >
97+
98+ ``` c++
99+ Semaphore::P () {
100+ sem--;
101+ if (sem < 0) {
102+ Add this thread t to q;
103+ block (t)
104+ }
105+ }
106+ ```
107+
108+ </div >
109+
110+ <div class =" col " >
111+
112+ ``` c++
113+ Semaphore::V () {
114+ sem++;
115+ if (sem <= 0) {
116+ Remove a thread t from q;
117+ wakeup (t)
118+ }
119+ }
120+ ```
121+
122+ </div >
123+
124+ </div >
125+
126+ </font >
127+
128+ ---
129+
130+
131+ ### 信号量在概念上的实现
132+
133+ <font size =6 >
134+
135+ ```
136+ Class Semaphore {
137+ int sem;
138+ WaitQueue q;
139+ }
140+ ```
141+
142+ <style >
143+ .container {
144+ display : flex ;
145+ }
146+ .col {
147+ flex : 1 ;
148+ }
149+ </style >
150+
151+ <div class =" container " >
152+
153+ <div class =" col " >
154+
155+ ``` c++
156+ Semaphore::P () {
157+ while (sem <= 0) {
158+ Add this thread t to q;
159+ block (t)
160+ }
161+ sem--;
162+ }
163+ ```
164+
165+ </div >
166+
167+ <div class =" col " >
168+
169+ ``` c++
170+ Semaphore::V () {
171+ sem++;
172+ if (sem <= 0) {
173+ Remove a thread t from q;
174+ wakeup (t)
175+ }
176+ }
177+ ```
178+
179+ </div >
180+
181+ </div >
182+
183+ 问题:这个实现与上一个有什么不同?
68184
185+ </font >
69186
70187---
71188
You can’t perform that action at this time.
0 commit comments