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

2019-08-08 21:31发布

问题:

What is the differance between boost::details::pool::pthread_mutex and boost::details::pool::null_mutex.

I see that in latest boost version - 1.42, the class boost::details::pool::pthread_mutex was deleted. What should I use instead?

回答1:

boost::details::pool::null_mutex is a mutex that does nothing (a lock always succeeds immediately). It's appropriate when you're not using threads. The Boost pool library selects what kind of mutex it will use to synchronize access to critical sections with a typedef for the mutex type based on the following snippet from boost\pool\detail\mutex.hpp:

#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

In other words, if the configuration says that no threading is involved (either for Boost as a whole, or for the pool library in particular), then the null_mutex will be used (which is basically a nop).

If threading is to be supported, then the boost::mutex type will be used, which comes from the Boost thread library (and will be a pthread-based mutex if your system uses pthreads).