wrapping boost::mutex::scoped_lock

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

jmzorko@mac.com wrote:
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; };
Um. Maybe I'm missing something here, but this doesn't make any sense to me at all. The whole point of scoped_lock is that the lifetime of the mutex and scoped_lock lifetimes are *different*. Furthermore, you're not actually locking anything with your Lock class... you'd simply get a different mutex for each instance of Lock you create. In short: You'd need two wrapper classes, one for scoped_lock and one for mutex. Which means you might as well just use boost::mutex and boost::scoped_lock. Cheers, -- Bardur Arantsson <bardurREMOVE@THISscientician.net> - That which does not kill you only makes you stranger. MisterQueue @ http://kuro5hin.org

Bardur,
Um. Maybe I'm missing something here, but this doesn't make any sense to me at all. The whole point of scoped_lock is that the lifetime of the mutex and scoped_lock lifetimes are *different*. Furthermore, you're not actually locking anything with your Lock class... you'd simply get a different mutex for each instance of Lock you create.
In short: You'd need two wrapper classes, one for scoped_lock and one for mutex. Which means you might as well just use boost::mutex and boost::scoped_lock.
That was it exactly ... makes perfect sense, and now my unit test works. Many thanks! Regards, John Falling You - exploring the beauty of voice and sound http://www.fallingyou.com
participants (2)
-
Bardur Arantsson
-
jmzorko@mac.com