We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a29e3e0 commit f0d8efcCopy full SHA for f0d8efc
lec12/p3-monitor-cond.md
@@ -398,3 +398,23 @@ https://yangzhaoyunfei.github.io/monitors/ 管程(Monitors) -->
398
#### 生产者-消费者问题的管程实现
399
400

401
+
402
403
+---
404
+#### 如何使用管程?
405
406
+Java支持管程;C++支持条件变量
407
408
+```
409
+public class CounterMonitor {
410
+ private int count = 0; // 共享资源
411
+ // 增加count的方法,这里用synchronized关键字确保互斥访问
412
+ public synchronized void increment() {
413
+ count++; // 可以添加其他逻辑,比如通知等待的线程等
414
+ }
415
+ // 获取count值的方法,也需要同步
416
+ public synchronized int getCount() {
417
+ return count;
418
419
+}
420
+'''
0 commit comments