473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Lock pointer implementation

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<Tp,
boost::shared_ptr<boost::recursive_mutexmutex)
: p_(p), lock_(new boost::recursive_mutex::scoped_lock(*mutex))
{
}

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

private:
boost::shared_ptr<Tp_;
boost::shared_ptr<boost::recursive_mutex::scoped_l ocklock_;
};

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

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

private:
boost::shared_ptr<Tp_;
boost::shared_ptr<boost::recursive_mutexmutex_;
};

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
Sep 7 '06 #1
2 3241
Mattias Brändström" <"thebrasse at brasse dot org wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '06 #2
Mattias Brändström wrote:
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.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Sep 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
17
by: euan_woo | last post by:
Hi, Sometimes my program stops and when I break it I see 2 threads both waiting at a lock statement trying to lock the same object. If I look up the call stack of these threads there aren't any...
17
by: djc | last post by:
I got great info on related question previously. This link <http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian Gideon was especially informative. 1) the lock statement does...
190
by: blangela | last post by:
If you had asked me 5 years ago about the future of C++, I would have told you that its future was assured for many years to come. Recently, I have been starting to wonder. I have been teaching...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.