c++读写锁的实现

一、使用互斥锁和条件变量实现读写锁:

  1. class readwrite_lock  
  2. {  
  3. public:  
  4.     readwrite_lock()  
  5.         : stat(0)  
  6.     {  
  7.     }  
  8.   
  9.     void readLock()  
  10.     {  
  11.         mtx.lock();  
  12.         while (stat < 0)  
  13.             cond.wait(mtx);  
  14.         ++stat;  
  15.         mtx.unlock();  
  16.     }  
  17.   
  18.     void readUnlock()  
  19.     {  
  20.         mtx.lock();  
  21.         if (--stat == 0)  
  22.             cond.notify_one(); // 叫醒一个等待的写操作  
  23.         mtx.unlock();  
  24.     }  
  25.   
  26.     void writeLock()  
  27.     {  
  28.         mtx.lock();  
  29.         while (stat != 0)  
  30.             cond.wait(mtx);  
  31.         stat = -1;  
  32.         mtx.unlock();  
  33.     }  
  34.   
  35.     void writeUnlock()  
  36.     {  
  37.         mtx.lock();  
  38.         stat = 0;  
  39.         cond.notify_all(); // 叫醒所有等待的读和写操作  
  40.         mtx.unlock();  
  41.     }  
  42.   
  43. private:  
  44.     mutex mtx;  
  45.     condition_variable cond;  
  46.     int stat; // == 0 无锁;> 0 已加读锁个数;< 0 已加写锁  
  47. };  


二、使用2个互斥锁实现读写锁:

  1. class readwrite_lock  
  2. {  
  3. public:  
  4.     readwrite_lock()  
  5.         : read_cnt(0)  
  6.     {  
  7.     }  
  8.   
  9.     void readLock()  
  10.     {  
  11.         read_mtx.lock();  
  12.         if (++read_cnt == 1)  
  13.             write_mtx.lock();  
  14.   
  15.         read_mtx.unlock();  
  16.     }  
  17.   
  18.     void readUnlock()  
  19.     {  
  20.         read_mtx.lock();  
  21.         if (--read_cnt == 0)  
  22.             write_mtx.unlock();  
  23.   
  24.         read_mtx.unlock();  
  25.     }  
  26.   
  27.     void writeLock()  
  28.     {  
  29.         write_mtx.lock();  
  30.     }  
  31.   
  32.     void writeUnlock()  
  33.     {  
  34.         write_mtx.unlock();  
  35.     }  
  36.   
  37. private:  
  38.     mutex read_mtx;  
  39.     mutex write_mtx;  
  40.     int read_cnt; // 已加读锁个数  
  41. };  


用mutex和conditon实现写优先的读写锁

  1. class RWLock {  
  2. private:  
  3.     pthread_mutex_t mxt;  
  4.     pthread_cond_t cond;  
  5.     int rd_cnt;//等待读的数量  
  6.     int wr_cnt;//等待写的数量  
  7.   
  8. public:  
  9.     RWLock() :rd_cnt(0), wr_cnt(0) {  
  10.         pthread_mutex_init(&mxt,NULL);  
  11.         pthread_cond_init(&cond,NULL);  
  12.     }  
  13.   
  14.     void readLock() {  
  15.         pthread_mutex_lock(&mxt);  
  16.   
  17.         ++rd_cnt;  
  18.         while(wr_cnt > 0)  
  19.             pthread_mutex_wait(&cond, &mxt);  
  20.           
  21.         pthread_mutex_unlock(&mxt);  
  22.     }  
  23.   
  24.     void readUnlock() {  
  25.         pthread_mutex_lock(&mxt);  
  26.           
  27.         --rd_cnt;  
  28.         if (rd_cnt == 0 )  
  29.             pthread_cond_signal(&cond);  
  30.   
  31.         pthread_mutex_unlock(&mxt);  
  32.     }  
  33.   
  34.     void writeLock() {  
  35.         pthread_mutex_lock(&mxt);  
  36.   
  37.         ++wr_cnt;  
  38.         while (wr_cnt + rd_cnt >=2)  
  39.             pthread_cond_wait(&cond, &mxt);  
  40.   
  41.         pthread_mutex_unlock(&mxt);  
  42.     }  
  43.   
  44.     void writerUnlock() {  
  45.         pthread_mutex_lock(&mxt);  
  46.   
  47.         --wr_cnt;  
  48.         if(wr_cnt==0)  
  49.             pthread_cond_signal(&cond);  
  50.   
  51.         pthread_mutex_unlock(&mxt);  
  52.     }  
  53. }; 

猜你喜欢

转载自eric-gcm.iteye.com/blog/2162288