读写锁优先级 写饥饿
2015-08-23 15:55
323 查看
对一个同享的数据布局,读的频率远弘远于写,所以用了读写锁.但是发现写线程老是抢不到锁.
按The Open Group 的Single UNIX? Specification所说,"Thepthread_rwlock_rdlock() function applies a read lock to the read-write lock referenced by rwlock. The calling thread acquires the read lock if a writer does not hold the lock and there are no writers blocked on the lock. It is unspecified whether the calling thread acquires the lock when a writer does not hold the lock and there are writers waiting for the lock" 貌似意思是说,没有writer在持有写锁的时,reader是可以拿到读锁的。然则没有划定,若是有writer在等写锁,该若何?
自己测试结果:
对于ACE_RW_Mutex,当几个线程不断获取释放读锁,各读线程使用锁的时间叠加,锁讲一直处于被读取状态,此时加写锁,写线程将一直处于等待状态。
即ACE_RW_Mutex 并不保证读写锁请求和处理时序。读锁获条件是只要当前锁没有被别人以写方式获取就可以获取到读权限。
这样做的优势是由于读锁可并发执行,提高CPU利用率。缺点是在读锁使用频度较高情况下,会出现不断有线程使用读锁,写锁线程饥饿等待。
pthread_rwlockattr_setkind_np 应该可以调整读写锁优先级顺序。
按The Open Group 的Single UNIX? Specification所说,"Thepthread_rwlock_rdlock() function applies a read lock to the read-write lock referenced by rwlock. The calling thread acquires the read lock if a writer does not hold the lock and there are no writers blocked on the lock. It is unspecified whether the calling thread acquires the lock when a writer does not hold the lock and there are writers waiting for the lock" 貌似意思是说,没有writer在持有写锁的时,reader是可以拿到读锁的。然则没有划定,若是有writer在等写锁,该若何?
自己测试结果:
对于ACE_RW_Mutex,当几个线程不断获取释放读锁,各读线程使用锁的时间叠加,锁讲一直处于被读取状态,此时加写锁,写线程将一直处于等待状态。
即ACE_RW_Mutex 并不保证读写锁请求和处理时序。读锁获条件是只要当前锁没有被别人以写方式获取就可以获取到读权限。
这样做的优势是由于读锁可并发执行,提高CPU利用率。缺点是在读锁使用频度较高情况下,会出现不断有线程使用读锁,写锁线程饥饿等待。
pthread_rwlockattr_setkind_np 应该可以调整读写锁优先级顺序。
相关文章推荐
- php大力力 [007节]php静态表量
- hibernate Criteria Query
- android权限大全
- Pascal数组递推
- 阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+部分答案)
- hdu1527 威佐夫博奕
- iOS 自定义动画 push/pop动画
- JQuery第四天总结
- HDOJ 5419 Victor and Toys 树状数组
- oracle 查看用户所在的表空间
- Objective-C 【多态】
- 设计模式之工厂模式(3)
- Objective-C 【多态】
- STM32标准外设库学习笔记-20150823-阅读stm32f10x.h
- android LayoutInflater三种获取方式
- webapp的优化总结
- servlet第2讲(下集)----通过HttpServlet实现一个用户登录网站(继承HttpServlet)
- JavaScript表单验证
- leetcode 162 —— Find Peak Element
- 学习日志8.23(附加转载)