Lock pointer implementation

G

Guest

Hello!

I have a small class that I would like to get some feedback on. If you
have the time. =)

What I would like to do is to protect an instance of some class T with a
mutex. It seems to me that I would be able to do this with a smart
pointer that returns a proxy class, from operator->(), that locks a
mutex in its constructor and unlocks it in its destrcutor. This is my
intitial implementation of such a class:

#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>

template <typename T>
class LockPointer
{
public:

class LockProxy
{
public:
LockProxy(boost::shared_ptr<T> p,
boost::shared_ptr<boost::recursive_mutex> mutex)
: p_(p), lock_(new boost::recursive_mutex::scoped_lock(*mutex))
{
}

boost::shared_ptr<T> operator->()
{
return p_;
}

private:
boost::shared_ptr<T> p_;
boost::shared_ptr<boost::recursive_mutex::scoped_lock> lock_;
};

LockPointer(boost::shared_ptr<T> o)
: p_(o), mutex_(new boost::recursive_mutex())
{
}

LockProxy operator->()
{
return LockProxy(p_, mutex_);
}

private:
boost::shared_ptr<T> p_;
boost::shared_ptr<boost::recursive_mutex> mutex_;
};

What do you think? Would this work? Would this be equivalent to locking
a member mutex in each method of T (as long as all calls to an instance
of T is invoked via the same LockPointer)?

For some reason this is the kind of thing I would expect to find in
boost, but I have not been able to. Have I missed some part of boost
that I should know about?

Regards,
Mattias
 
V

Victor Bazarov

Mattias said:
I have a small class that I would like to get some feedback on. If you
have the time. =)

What I would like to do is to protect an instance of some class T
with a mutex. It seems to me that I would be able to do this with a
smart pointer that returns a proxy class, from operator->(), that
locks a mutex in its constructor and unlocks it in its destrcutor.
This is my intitial implementation of such a class:

[..snip..]

Your code doesn't seem to have the destructor where the mutex would be
unlocked.
What do you think? Would this work? [..]

Why ask? Try it. If it works for you, use it.

From the C++ language standpoint mutex is undefined, so we can't really
look at the functionality of your code from C++ *language* point of view.
For some reason this is the kind of thing I would expect to find in
boost, but I have not been able to. Have I missed some part of boost
that I should know about?

Perhaps you missed the most important part: Boost has its own discussion
forums where you should post your question.

V
 
J

Joe Seigh

Mattias said:
Hello!

I have a small class that I would like to get some feedback on. If you
have the time. =)

What I would like to do is to protect an instance of some class T with a
mutex. It seems to me that I would be able to do this with a smart
pointer that returns a proxy class, from operator->(), that locks a
mutex in its constructor and unlocks it in its destrcutor. This is my
intitial implementation of such a class:
....

What do you think? Would this work? Would this be equivalent to locking
a member mutex in each method of T (as long as all calls to an instance
of T is invoked via the same LockPointer)?

For some reason this is the kind of thing I would expect to find in
boost, but I have not been able to. Have I missed some part of boost
that I should know about?

It's been tried before and isn't a generally useful function for the
same reason there's no equivalent wrapper class in Java that makes
every method a synchronized method. There are synchronized wrapper
classes in Java but they are target class specific and they do not
make every method in the target class a syncrhonized one.

No silver bullet. Especially as far as threading is concerned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top