Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2006-11-01 16:15:17


On Nov 1, 2006, at 4:06 PM, Roland Schwarz wrote:

> So we would end up with mutex and recursive_mutex, yes?

Instead of a recursive_mutex, how would you feel about a
recursive_mutex *adaptor* (like stack is to containers). Here's a
prototype. Warning, it is not mature:

template <class Mutex>
class recursive_mutex
{
private:
     Mutex& mut_;
     unsigned count_;
     thread_util::id id_;
public:
     explicit recursive_mutex(Mutex& mut)
         : mut_(mut), count_(0), id_(thread_self::not_any_thread()) {}
     void lock()
     {
         thread_self::id self = thread_self::current();
         if (thread_self::equal(id_, self))
             ++count_;
         else
         {
             mut_.lock();
             id_ = self;
             store_release(&count_, 1);
         }
     }

     void unlock()
     {
         if (--count_ == 0)
         {
             id_ = thread_self::not_any_thread();
             mut_.unlock();
         }
     }
};

The basic idea is a mutex *adaptor*. Everything else I have wrong
above is just details. ;-)

-Howard


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