boost::details::pool::pthread_mutex and boost::details::pool

boost::details::pool::pthread_mutex and boost::details::pool::null_mutex

本文关键字:boost pool details and mutex pthread      更新时间:2023-10-16

boost::details::pool::pthread_mutexboost::details::pool::null_mutex有什么区别。

我看到在最新的提升版本 - 1.42 中,类boost::details::pool::pthread_mutex被删除了。我应该改用什么?

boost::details::pool::null_mutex是一个互斥锁,什么都不做(锁总是立即成功)。 当您不使用线程时,这是合适的。 Boost 池库根据 boostpooldetailmutex.hpp 中的以下代码片段,选择它将使用哪种类型的互斥锁来同步对关键部分的访问与互斥锁类型的 typedef:

#if !defined(BOOST_HAS_THREADS) || defined(BOOST_NO_MT) || defined(BOOST_POOL_NO_MT)
  typedef null_mutex default_mutex;
#else
  typedef boost::mutex default_mutex;
#endif

换句话说,如果配置说不涉及线程(无论是对于整个 Boost,还是特别是对于池库),那么将使用null_mutex(这基本上是一个 nop)。

如果要支持线程,则将使用 boost::mutex 类型,该类型来自 Boost 线程库(如果您的系统使用 pthreads,它将是基于 pthread的互斥锁)。