Re: Promoting weak pointers

Andrew Tomazos <andrew@tomazos.com>
Tue, 16 Jun 2009 03:39:49 -0700 (PDT)

          From comp.compilers

Related articles
Promoting weak pointers m.helvensteijn@gmail.com (Michiel Helvensteijn) (2009-06-11)
Re: Promoting weak pointers fset.slb@gmail.com (Scott Burson) (2009-06-15)
Re: Promoting weak pointers andrew@tomazos.com (Andrew Tomazos) (2009-06-16)
Re: Promoting weak pointers m.helvensteijn@gmail.com (Michiel Helvensteijn) (2009-06-16)
Re: Promoting weak pointers m.helvensteijn@gmail.com (Michiel Helvensteijn) (2009-06-16)
Re: Promoting weak pointers m.helvensteijn@gmail.com (Michiel Helvensteijn) (2009-06-17)
Re: Promoting weak pointers bobduff@shell01.TheWorld.com (Robert A Duff) (2009-06-17)
Re: Promoting weak pointers andrew@tomazos.com (Andrew Tomazos) (2009-06-18)
Re: Promoting weak pointers armelasselin@hotmail.com (Armel) (2009-06-19)
[1 later articles]
| List of all articles for this month |
From: Andrew Tomazos <andrew@tomazos.com>
Newsgroups: comp.compilers
Date: Tue, 16 Jun 2009 03:39:49 -0700 (PDT)
Organization: Compilers Central
References: 09-06-047
Keywords: linker
Posted-Date: 16 Jun 2009 07:05:48 EDT

On Jun 11, 6:10 pm, Michiel Helvensteijn <m.helvenste...@gmail.com>
wrote:
> So the main question would be: Can anyone think of a use-case for
> promotion of a weak pointer in which there is no obvious alternative?


One reason you may want to promote a weak reference to a strong
reference is under contention from different thread contexts. In fact
where there is contention, the system should *require* promotion
before dereferencing.


Consider:


      if (my_weak_ptr != NULL)
                my_weak_ptr->do_something();


Here if my_weak_ptr's target is garbage collected in another thread
between the test and when it is used than you are going to dereference
null.


Better to:


        my_strong_ptr = my_weak_ptr;
        if (my_strong_ptr != NULL)
                my_strong_ptr->do_something();


That way the strong reference will provide protection for the critical
section of code.


Enjoy,
Andrew.
--
Andrew Tomazos <andrew@tomazos.com> <http://www.tomazos.com>
[In every system I've seen with strong and weak pointers, the pointer
values are all either filled in or not before the program starts. I
suppose there might be race conditions if you do dynamic linking, but
that's a separate issue. -John]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.