1. 程式人生 > >zthread學習 例項十 執行緒間的協助(四)——讀者、寫者

zthread學習 例項十 執行緒間的協助(四)——讀者、寫者

對於讀寫問題,ZThread庫專門封裝了類(ReadWriteLock)來控制讀寫:

FairReadWriteLock (按FIFO的順序進行讀寫控制):

 A FairReadWriteLock maintains a balance between the order read-only access and read-write access is allowed. Threads contending for the pair of Lockable objects this ReadWriteLock provides will gain access to the locks in FIFO order.

BiasedReadWriteLock(寫優先的讀寫控制):

A BiasedReadWriteLock has a bias toward writers. It will prefer read-write access over read-only access when many threads are contending for access to either Lockable this ReadWriteLock provides.

下面是試測試程式碼(文中帶有註釋,因此後面不再分析程式):

 

使用FairReadWriteLock時的執行結果:使用BiasedReadWriteLock時的執行結果: