boost库的共享mutex实际上很简单,它是由条件变量、普通的mutex构成的。shared_mutex维护了2个条件变量来判断是否可读、可写。按照以下原则进行加锁和解锁:
- 写标志位:unsigned int 最高位为1则表示已经进行写加锁
- 读标志位:unsigned int 最高位以外的所有为不为1则表示存在读锁,读锁的个数就是该 unsigned int 变量的值
- 写加锁 :当不存在写锁、读锁的时候可以获取写锁,否则一直等待
- 读加锁 :不存在写锁的时候可以进行加锁,否则一直等待。读锁可以多次加锁,每加一次则读锁计数加一
- 锁释放 :写锁释放时
- shared_lock: boost::shared_mutex 可以配合 boost::unique_lock boost::guard_lock 以及 shared_lock 使用。惟一的区别是,shared_lock 是获取读锁,其他的是获取写锁。
存在的问题:
- 写锁饥饿:由于写锁需要等待所有读锁解锁完毕后才能获取,因此当一个线程尝试获取写锁的时候,其他的线程一直进行读锁获取,会导致写锁饥饿的情况。按照boost的共享锁的实现,它是一个读优先锁
- 锁唤醒 :当尝试加写锁的线程由于不满足条件而进行条件变量wait之后,其他线程释放写锁之后由于shared_mutex中没有写锁等待的计数因此不知道是否有写锁存在,因此只能选择唤醒所有其他的条件变量。更好的办法是对当前读写锁分别进行计数,根据读写锁的数量来选择性的唤醒不同的锁。由于有了读写计数,则可以很方便的选择唤醒哪个条件变量,这样还可以实现读写优先锁。写优先级的读写锁可以参见《读写锁、自旋锁、信号量的CPP11实现》。
namespace boost {
namespace thread_v2 {
class shared_mutex
{
typedef boost::mutex mutex_t;
typedef boost::condition_variable cond_t;
typedef unsigned count_t;
mutex_t mut_;
cond_t gate1_;
// the gate2_ condition variable is only used by functions that
// have taken write_entered_ but are waiting for no_readers()
cond_t gate2_;
count_t state_;
static const count_t write_entered_ = 1U << (sizeof(count_t)*CHAR_BIT - 1);
static const count_t n_readers_ = ~write_entered_;
bool no_writer() const
{
return (state_ & write_entered_) == 0;
}
bool one_writer() const
{
return (state_ & write_entered_) != 0;
}
bool no_writer_no_readers() const
{
//return (state_ & write_entered_) == 0 &&
// (state_ & n_readers_) == 0;
return state_ == 0;
}
bool no_writer_no_max_readers() const
{
return (state_ & write_entered_) == 0 &&
(state_ & n_readers_) != n_readers_;
}
bool no_readers() const
{
return (state_ & n_readers_) == 0;
}
bool one_or_more_readers() const
{
return (state_ & n_readers_) > 0;
}
shared_mutex(shared_mutex const&);
shared_mutex& operator=(shared_mutex const&);
public:
shared_mutex();
~shared_mutex();
// Exclusive ownership
void lock();
bool try_lock();
#ifdef BOOST_THREAD_USES_CHRONO
template <class Rep, class Period>
bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time)
{
return try_lock_until(chrono::steady_clock::now() + rel_time);
}
template <class Clock, class Duration>
bool try_lock_until(
const boost::chrono::time_point<Clock, Duration>& abs_time);
#endif
#if defined BOOST_THREAD_USES_DATETIME
template<typename T>
bool timed_lock(T const & abs_or_rel_time);
#endif
void unlock();
// Shared ownership
void lock_shared();
bool try_lock_shared();
#ifdef BOOST_THREAD_USES_CHRONO
template <class Rep, class Period>
bool try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time)
{
return try_lock_shared_until(chrono::steady_clock::now() + rel_time);
}
template <class Clock, class Duration>
bool try_lock_shared_until(
const boost::chrono::time_point<Clock, Duration>& abs_time);
#endif
#if defined BOOST_THREAD_USES_DATETIME
template<typename T>
bool timed_lock_shared(T const & abs_or_rel_time);
#endif
void unlock_shared();
};
inline shared_mutex::shared_mutex()
: state_(0)
{
}
inline shared_mutex::~shared_mutex()
{
boost::lock_guard<mutex_t> _(mut_);
}
// Exclusive ownership
inline void shared_mutex::lock()
{
boost::unique_lock<mutex_t> lk(mut_);
gate1_.wait(lk, boost::bind(&shared_mutex::no_writer, boost::ref(*this)));
state_ |= write_entered_;
gate2_.wait(lk, boost::bind(&shared_mutex::no_readers, boost::ref(*this)));
}
inline bool shared_mutex::try_lock()
{
boost::unique_lock<mutex_t> lk(mut_);
if (!no_writer_no_readers())
{
return false;
}
state_ = write_entered_;
return true;
}
#ifdef BOOST_THREAD_USES_CHRONO
template <class Clock, class Duration>
bool shared_mutex::try_lock_until(
const boost::chrono::time_point<Clock, Duration>& abs_time)
{
boost::unique_lock<mutex_t> lk(mut_);
if (!gate1_.wait_until(lk, abs_time, boost::bind(
&shared_mutex::no_writer, boost::ref(*this))))
{
return false;
}
state_ |= write_entered_;
if (!gate2_.wait_until(lk, abs_time, boost::bind(
&shared_mutex::no_readers, boost::ref(*this))))
{
state_ &= ~write_entered_;
return false;
}
return true;
}
#endif
#if defined BOOST_THREAD_USES_DATETIME
template<typename T>
bool shared_mutex::timed_lock(T const & abs_or_rel_time)
{
boost::unique_lock<mutex_t> lk(mut_);
if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
&shared_mutex::no_writer, boost::ref(*this))))
{
return false;
}
state_ |= write_entered_;
if (!gate2_.timed_wait(lk, abs_or_rel_time, boost::bind(
&shared_mutex::no_readers, boost::ref(*this))))
{
state_ &= ~write_entered_;
return false;
}
return true;
}
#endif
inline void shared_mutex::unlock()
{
boost::lock_guard<mutex_t> _(mut_);
BOOST_ASSERT(one_writer());
BOOST_ASSERT(no_readers());
state_ = 0;
// notify all since multiple *lock_shared*() calls may be able
// to proceed in response to this notification
gate1_.notify_all();
}
// Shared ownership
inline void shared_mutex::lock_shared()
{
boost::unique_lock<mutex_t> lk(mut_);
gate1_.wait(lk, boost::bind(&shared_mutex::no_writer_no_max_readers, boost::ref(*this)));
count_t num_readers = (state_ & n_readers_) + 1;
state_ &= ~n_readers_;
state_ |= num_readers;
}
inline bool shared_mutex::try_lock_shared()
{
boost::unique_lock<mutex_t> lk(mut_);
if (!no_writer_no_max_readers())
{
return false;
}
count_t num_readers = (state_ & n_readers_) + 1;
state_ &= ~n_readers_;
state_ |= num_readers;
return true;
}
#ifdef BOOST_THREAD_USES_CHRONO
template <class Clock, class Duration>
bool shared_mutex::try_lock_shared_until(
const boost::chrono::time_point<Clock, Duration>& abs_time)
{
boost::unique_lock<mutex_t> lk(mut_);
if (!gate1_.wait_until(lk, abs_time, boost::bind(
&shared_mutex::no_writer_no_max_readers, boost::ref(*this))))
{
return false;
}
count_t num_readers = (state_ & n_readers_) + 1;
state_ &= ~n_readers_;
state_ |= num_readers;
return true;
}
#endif
#if defined BOOST_THREAD_USES_DATETIME
template<typename T>
bool shared_mutex::timed_lock_shared(T const & abs_or_rel_time)
{
boost::unique_lock<mutex_t> lk(mut_);
if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
&shared_mutex::no_writer_no_max_readers, boost::ref(*this))))
{
return false;
}
count_t num_readers = (state_ & n_readers_) + 1;
state_ &= ~n_readers_;
state_ |= num_readers;
return true;
}
#endif
inline void shared_mutex::unlock_shared()
{
boost::lock_guard<mutex_t> _(mut_);
BOOST_ASSERT(one_or_more_readers());
count_t num_readers = (state_ & n_readers_) - 1;
state_ &= ~n_readers_;
state_ |= num_readers;
if (no_writer())
{
if (num_readers == n_readers_ - 1)
gate1_.notify_one();
}
else
{
if (num_readers == 0)
gate2_.notify_one();
}
}
} // thread_v2
} // boost
文档信息
- 本文作者:will kall
- 本文链接:https://fishlovee.github.io/2020/07/03/03-boost%E5%85%B1%E4%BA%AB%E9%94%81%E7%9A%84%E5%AE%9E%E7%8E%B0/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)