From: moerchendiser2k3 on
Hi,

I have a common question about locks:

class SetPointer
{
private:
void *ptr;

MY_LOCK lock;


public:
void SetPointer(void *p)
{
Lock(this->lock);
this->ptr = p;
}

void *GetPointer()
{
Lock(this->lock);
return this->ptr;
}
};


Just a question, is this lock redundant, when the Pointer can be set/
get from different threads?
Thanks a lot!! Bye, moerchendiser2k3
From: Ben Finney on
moerchendiser2k3 <googler.1.webmaster(a)spamgourmet.com> writes:

> I have a common question about locks:

You'd be best to ask in a forum related to the language you're using.
This ('comp.lang.python') is a forum for users of the Python language.

--
\ “If you can do no good, at least do no harm.” —_Slapstick_, |
`\ Kurt Vonnegut |
_o__) |
Ben Finney
From: Chris Rebert on
On Fri, Mar 19, 2010 at 6:58 PM, moerchendiser2k3
<googler.1.webmaster(a)spamgourmet.com> wrote:
<question about using locks in C++ snipped>
>
> Thanks a lot!! Bye, moerchendiser2k3

This is the **Python** mailinglist/newsgroup; and your question isn't
about Python.

The C++ one is over there:
http://groups.google.com/group/comp.lang.c++.moderated/topics

Regards,
Chris
From: MRAB on
moerchendiser2k3 wrote:
> Hi,
>
> I have a common question about locks:
>
> class SetPointer
> {
> private:
> void *ptr;
>
> MY_LOCK lock;
>
>
> public:
> void SetPointer(void *p)
> {
> Lock(this->lock);
> this->ptr = p;
> }
>
> void *GetPointer()
> {
> Lock(this->lock);
> return this->ptr;
> }
> };
>
>
> Just a question, is this lock redundant, when the Pointer can be set/
> get from different threads?
> Thanks a lot!! Bye, moerchendiser2k3

1. That's C++. What does it have to do with Python?

2. The value you're accessing is a simple pointer which you're either
setting or getting, so a lock shouldn't be necessary.

3. You're locking, but never unlocking. The sequence should be: lock, do
stuff, unlock.
From: Gabriel Genellina on
En Fri, 19 Mar 2010 23:31:23 -0300, MRAB <python(a)mrabarnett.plus.com>
escribi�:
> moerchendiser2k3 wrote:

>> class SetPointer
>> {
>> private:
>> void *ptr;
>> MY_LOCK lock;
>> public:
>> void SetPointer(void *p)
>> {
>> Lock(this->lock);
>> this->ptr = p;
>> }

> 3. You're locking, but never unlocking. The sequence should be: lock, do
> stuff, unlock.

Just FYI: C++ doesn't have try/finally, and such behavior is usually
emulated using a local object. When it goes out of scope, it is
automatically destroyed, meaning that the object destructor is called.
Whatever you would write in a "finally" clause, in C++ goes into a
destructor.

Of course C++ guys would never say they're "emulating" try/finally,
instead they declare RAII as *the* Only and Right Way :)

--
Gabriel Genellina