[已软注销]对《The Little Book of Semaphores, 2nd Edition》的笔记(2)
[已软注销] (Hello world)
-
第3页 Serialization with messages
-
3.8.2 FIFO
3.8.2节有错,这节的题目是用Semaphore实现FIFO的Semaphore,给出的最后答案是:
class Fifo: def __init__(self): self.queue= Queue() self.mutex = Semaphore(1) def wait(): self.mutex.wait() self.queue.add(mySem) sekf.mutex.signal() mySem.wait() def signal(): self.mutex.wait() sem = self.queue.remove() self.mutex.signal() sem.signal()
给出题目时要求我们不要把Queue的操作当作thread-safe的,所以需要mutex来保护queue的操作,但是python的Queue的出队列方法有一个特殊的语义:如果队列为空,调用出队操作的进程阻塞,这样一来上面的程序就会造成死锁:在可能阻塞的操作里得到一个mutex,而之后入队线程又必须获得这个mutex。所以这个类只能一个一个入队完之后才能一个一个出队。所以这个类在实际中没什么作用。 这样来说这本书里的内容实在是对实际的编程没有什么作用,最大的作用恐怕就是锻炼下思维以及在面试之前看看了吧。 ps. 就不吐槽mySem根本没有初始化直接使用,也不吐槽类的方法连self参数都没有,也不吐槽python里Queue的方法和Semaphore的方法名根本就不对(不过Semaphore的方法名竟然是acquire和release就是python的不对了)
[已软注销]的其他笔记 · · · · · · ( 全部82条 )
- 论美国的民主
- 1
- Big Debt Crises
- 1
- 论美国的民主
- 1
- The Defining Decade
- 1
- In The Plex
- 1
- Verbal Advantage
- 2
- Introduction to Algorithms (3/e)
- 1
- Merriam-Webster's Vocabulary Builder
- 1
- Programming Erlang, Second Edition
- 1
- Capital in the Twenty First Century
- 1
- Programming Clojure
- 1
- 编程珠玑
- 1
- 我们都要性小康
- 1
- Haskell趣学指南
- 1
- The Joy of Clojure
- 1
- 经济为什么会崩溃
- 1
- ZeroMQ
- 2
- 通往奴役之路
- 1
- Linux Firewalls
- 1
- The Datacenter as a Computer
- 1
- 国富论
- 1
- 构建高性能Web站点
- 1
- Programming Pig
- 1
- HTTP权威指南
- 1
- flex & bison
- 1
- Understanding the Linux Virtual Memory Manager
- 2
- 依靠自我
- 1
- Operating Systems
- 9
- Structure and Interpretation of Computer Programs - 2nd Edition (MIT)
- 4
- Linux内核完全剖析
- 1
- TCP/IP基础教程基于实验的方法
- 1
- MongoDB
- 1
- 如彗星划过夜空
- 3
- Just for Fun
- 8
- 编译原理及实践
- 1
- TCP/IP详解 卷1:协议
- 1
- Coders at Work
- 4
- 什么是数学
- 1
- 那些忧伤的年轻人
- 3
- 我也有一个梦想
- 1
- 软件随想录
- 1
- Event Processing in Action
- 1
- FLEX 与 BISON(影印版)
- 1
- ANSI Common Lisp
- 1
- 黑客与画家
- 7
- 九型人格
- 1