Boost logo

Boost :

From: Jens Maurer (Jens.Maurer_at_[hidden])
Date: 2001-03-31 13:34:36


Jeremy Siek wrote:
>
> I think there may also be a cute way to do this using bitmasking...

> abraha> mutex_generator<checked, nonrecursive>::type

In order to show a possible interface of the generative approach,
I've sketched the implementation of a mutex_generator below.
The code is syntax-checked by como 4.2.45.2, but fails to
pass gcc 2.95.3. It passes some pre-release snapshot of the ongoing
gcc 3.0 development, though.

The specific implementation is meant for my glibc 2.2.2 platform.
The implementation of the generator looks different for each
platform, because POSIX offers less features "guaranteed".

The design of the mutex_linux<> template is debatable, but I
think the implementation variation for the various combinations
of supported mutex features is limited.

mutex_wrapped is the "condition variable" implementation
currently available in Bill Kempf's distributed .zip file.

Jens Maurer

#include <boost/pending/ct_if.hpp>
#include <boost/static_assert.hpp>

namespace boost {
namespace thread {

enum {
  recursive = (1 << 0),
  checking = (1 << 1),
  trylock_supported = (1 << 2),
  timedlock_supported = (1 << 3)
  // possibly more features
};

// declarations

namespace detail {
  template<int> class mutex_linux;
  class mutex_wrapped;
}

template<int features>
struct mutex_generator
{
  // this is for Linux, #ifdef for your platform

  // recursive and errorcheck are mutually exclusive
  BOOST_STATIC_ASSERT((features & (recursive|checking)) != (recursive|checking));

  // everything supports "trylock"
  // recusive and errorcheck don't support support "timedlock"
  typedef typename ct_if<
    (features & timedlock_supported),
    typename ct_if<
      (features & (recursive|checking)),
      detail::mutex_wrapped,
      detail::mutex_linux<timedlock_supported>
>::type,
    typename ct_if<
      features & recursive,
      detail::mutex_linux<recursive>,
      typename ct_if<
        features & checking,
        detail::mutex_linux<checking>,
        detail::mutex_linux<0>
>::type
>::type
>::type type;
};

} // namespace thread
} // namespace boost

// main.cpp

  typedef boost::thread::mutex_generator<boost::thread::trylock_supported | boost::thread::checking>::type mutex_type;
  mutex_type mtx;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk