Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #526 > unrolled thread

Thread Safety of "this" pointer

Started byittium <ittium@gmail.com>
First post2011-09-04 22:52 +0530
Last post2011-09-05 21:46 +0400
Articles 5 — 5 participants

Back to article view | Back to comp.programming.threads


Contents

  Thread Safety of "this" pointer ittium <ittium@gmail.com> - 2011-09-04 22:52 +0530
    Re: Thread Safety of "this" pointer David Schwartz <davids@webmaster.com> - 2011-09-04 15:31 -0700
      Re: Thread Safety of "this" pointer itt ium <ittium@gmail.com> - 2011-09-05 01:43 -0700
        Re: Thread Safety of "this" pointer ycwang <wangyongcong@gmail.com> - 2011-09-05 03:21 -0700
    Re: Thread Safety of "this" pointer Sergei Organov <osv@javad.com> - 2011-09-05 21:46 +0400

#526 — Thread Safety of "this" pointer

Fromittium <ittium@gmail.com>
Date2011-09-04 22:52 +0530
SubjectThread Safety of "this" pointer
Message-ID<j40c4l$gnt$1@dont-email.me>
Group,
There is a base class B and Two derived class D1 and D2. b is **shared** 
base class pointer. While d1 and d2 are derived class pointer

thread 1
--------
*b=d1;
b->execute();

thread 2
--------
*b=d2;

Should I protect (using mutex) the above code. Since this pointer will 
change during the processing
thanks
Ittium

[toc] | [next] | [standalone]


#527

FromDavid Schwartz <davids@webmaster.com>
Date2011-09-04 15:31 -0700
Message-ID<fcb78b32-03f6-4e84-a6b8-774ec5cca34b@m3g2000pri.googlegroups.com>
In reply to#526
On Sep 4, 10:22 am, ittium <itt...@gmail.com> wrote:
> Group,
> There is a base class B and Two derived class D1 and D2. b is **shared**
> base class pointer. While d1 and d2 are derived class pointer
>
> thread 1
> --------
> *b=d1;
> b->execute();
>
> thread 2
> --------
> *b=d2;
>
> Should I protect (using mutex) the above code. Since this pointer will
> change during the processing
> thanks
> Ittium

Yes. You must protected data with a mutex (or other synchronization
mechanism) if one thread might access it while another is, or might
be, modifying it.

DS

[toc] | [prev] | [next] | [standalone]


#529

Fromitt ium <ittium@gmail.com>
Date2011-09-05 01:43 -0700
Message-ID<e860f656-a223-4e28-a3dd-a96c39125934@n19g2000prh.googlegroups.com>
In reply to#527
On Sep 5, 3:31 am, David Schwartz <dav...@webmaster.com> wrote:
> On Sep 4, 10:22 am, ittium <itt...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > Group,
> > There is a base class B and Two derived class D1 and D2. b is **shared**
> > base class pointer. While d1 and d2 are derived class pointer
>
> > thread 1
> > --------
> > *b=d1;
> > b->execute();
>
> > thread 2
> > --------
> > *b=d2;
>
> > Should I protect (using mutex) the above code. Since this pointer will
> > change during the processing
> > thanks
> > Ittium
>
> Yes. You must protected data with a mutex (or other synchronization
> mechanism) if one thread might access it while another is, or might
> be, modifying it.
>
> DS

thanks for the quick reply

execute function in the above example is virtual that is implemented
by D1 and D2 both. Following are my doubt

Is b->execute() atomic i.e in the above situation, either execute of
D1 or D2 .

If the call is not atomic and not protected, what will be the
behavior. Are their chances of memory corruption.

What if one function starts executing that is "this pointer" that is
passed by value, is decided. and in the middle somebody changes the
base pointer will this affect execute call that is currently running
in other thread.

thanks
Ittium

[toc] | [prev] | [next] | [standalone]


#530

Fromycwang <wangyongcong@gmail.com>
Date2011-09-05 03:21 -0700
Message-ID<128de774-0204-4187-997e-c0f57ff7f7e6@br5g2000vbb.googlegroups.com>
In reply to#529
On Sep 5, 4:43 pm, itt ium <itt...@gmail.com> wrote:
> On Sep 5, 3:31 am, David Schwartz <dav...@webmaster.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Sep 4, 10:22 am, ittium <itt...@gmail.com> wrote:
>
> > > Group,
> > > There is a base class B and Two derived class D1 and D2. b is **shared**
> > > base class pointer. While d1 and d2 are derived class pointer
>
> > > thread 1
> > > --------
> > > *b=d1;
> > > b->execute();
>
> > > thread 2
> > > --------
> > > *b=d2;
>
> > > Should I protect (using mutex) the above code. Since this pointer will
> > > change during the processing
> > > thanks
> > > Ittium
>
> > Yes. You must protected data with a mutex (or other synchronization
> > mechanism) if one thread might access it while another is, or might
> > be, modifying it.
>
> > DS
>
> thanks for the quick reply
>
> execute function in the above example is virtual that is implemented
> by D1 and D2 both. Following are my doubt
>
> Is b->execute() atomic i.e in the above situation, either execute of
> D1 or D2 .
>
> If the call is not atomic and not protected, what will be the
> behavior. Are their chances of memory corruption.
>
> What if one function starts executing that is "this pointer" that is
> passed by value, is decided. and in the middle somebody changes the
> base pointer will this affect execute call that is currently running
> in other thread.
>
> thanks
> Ittium

Actually it do nothing with this pointer.

*b=d1 <=> (*b).operator = ( *((B*)d1) ), Commonly it copys d1's
members to *b

If b->execute() do not access any members, it's OK. Otherwise, you may
get wrong value because another thread has changed it.

[toc] | [prev] | [next] | [standalone]


#533

FromSergei Organov <osv@javad.com>
Date2011-09-05 21:46 +0400
Message-ID<j431tr$8e6$1@speranza.aioe.org>
In reply to#526
ittium <ittium@gmail.com> writes:

> Group,
> There is a base class B and Two derived class D1 and D2. b is
> **shared** base class pointer. While d1 and d2 are derived class
> pointer
>
> thread 1
> --------
> *b=d1;
> b->execute();
>
> thread 2
> --------
> *b=d2;
>
> Should I protect (using mutex) the above code. Since this pointer will
> change during the processing
> thanks
> Ittium

Yes, you should implement protection to the above code, but no, 'this'
pointer won't be changed during processing. It's contents of the
variable to which 'b' points that will be changed, and it's that
contents you need to protect (using mutex).

-- Sergei.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming.threads


csiph-web