Boost logo

Boost :

From: jmzorko_at_[hidden]
Date: 2006-06-08 17:26:04


Hello, all ...

I'm trying to wrap boost::mutex::scoped_lock in another class that
hides all of the boosty things:

     class Lock
     {
         public:

             Lock();
             ~Lock();

         private:

             boost::mutex mutex;
             boost::mutex::scoped_lock lock;
     };

... the idea is very similar to boost::mutex::scoped_lock -- that
being, acquire the lock when the Lock class is instantiated, and
release it when the Lock instance goes out of scope. However, i'm
having trouble getting this to happen. I tried:

Lock::Lock() : lock( mutex )
{
}

Lock::~Lock()
{
}

... to no avail (the lock is not active for the entire duration of
the Lock class instance, e.g. when the ctor is done, the lock is
released). I also tried making the lock on the heap:

     class Lock
     {
         public:

             Lock();
             ~Lock();

         private:

             boost::mutex mutex;
             boost::mutex::scoped_lock *plock;
     };

Lock::Lock() : plock( new boost::mutex:;scoped_lock( mutex ) )
{
}

Lock::~Lock()
{
     if ( plock ) delete plock;
}

... but this doesn't work, either -- the lock goes out of scope and
is released when the ctor finishes, not when the class itself goes
out of scope. I know i'm missing something, but I don't know what.

Regards,

John, who used to consider himself a fairly competent C++ developer,
but the last couple of years have been mainly Perl, so i'm anxious to
get the rust off

Falling You - exploring the beauty of voice and sound
http://www.fallingyou.com


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