在objc.io Issue 2中,作者讨论了读写方面的饥饿问题:
Just when you thought that there are enough problems to think of, a new one comes around the corner. Locking shared resources can result in the readers-writers problem. In many cases, it would be wasteful to restrict reading access to a resource to one access at a time. Therefore, taking a reading lock is allowed as long as there is no writing lock on the resource. In this situation, a thread that is waiting to acquire a write lock can be starved by more read locks occurring in the meantime.
我假设读取锁定仅允许锁定它的东西读取值,而其他所有对象都无法访问它?我知道您只能在没有写锁的情况下读取锁(因为值可能会发生变化),但是以这种方式使用它似乎暗示着“只有在没有写锁的情况下才允许读锁”被用作效率的手段/不浪费。读锁与浪费有什么关系?
另外,有人能解释一下最后一句话的意思吗?我不了解读锁会产生什么影响,或者读锁甚至会处于什么状态。
最佳答案
基本上,这意味着读锁定允许其他项目同时读取数据。
因此,请想象以下情形。
A取得读取锁并开始读取。
然后X出现并尝试采用写锁定,但是不能,因为它已经被锁定。
同时,在A完成读取之前,B出现并获得读取锁定。之所以允许这样做,是因为读锁可以是并发的。
然后,A完成,但是X仍然不能写,因为它被B锁定了。
然后,在B完成之前,C出现并取出读锁。等等。
关于multithreading - 我不明白饥饿是什么意思,正如作者所解释的那样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098017/