share_lock實現

C++17 才支持share_lock。

低版本的需要自己實現。

以下基於C++11實現。

源碼的地址:https://github.com/SaberMqd/naive/blob/master/include/naive/share_lock.h
 

#ifndef _NAIVE_SHARE_LOCK_H_
#define _NAIVE_SHARE_LOCK_H_

#ifdef _WIN32
#include <shared_mutex>
#else
#include <mutex>
#include <condition_variable>
#include <atomic>
#endif

namespace naive {

#ifdef _WIN32
	class ShareLock
	{
	public:
		ShareLock() {

		}

		void Lock() {
			_mtx.lock();
		}

		void UnLock() {
			_mtx.unlock();
		}

		void LockShared() {
			_mtx.lock_shared();
		}

		void UnLockShared() {
			_mtx.unlock_shared();
		}

		~ShareLock() {

		}

	private:
		std::shared_mutex _mtx;
	};
#else
	class ShareLock
	{
	public:
		ShareLock() : _pv(0), _writing(false), _hasWriting(false){ }
		
		void LockShared() {
			std::unique_lock<std::mutex> lock(_mtx);
			while (_writing == true) {
				_cv.wait(lock);
			}	
			++_pv;
		}

		void UnLockShared() {
			std::unique_lock<std::mutex> lock(_mtx);
			--_pv;			
			_cv.notify_one();
		}

		void Lock() {			
			std::unique_lock<std::mutex> lock(_mtx);
			_hasWriting = true;
			while ( _pv != 0 || _writing == true) {	
				_cv.wait(lock);
			}
			_writing = true;		
		}

		void UnLock() {
			std::unique_lock<std::mutex> lock(_mtx);
			_writing = false;
			_hasWriting = false;
			_cv.notify_all();
		}

		~ShareLock() { }

	private:
		std::mutex _mtx;
		std::condition_variable _cv;	
		std::atomic<int> _pv ; 
		bool _writing ;		 
		bool _hasWriting;
};

#endif

}

#endif // !_NAIVE_SHARE_LOCK_H_

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章