Re: Independent thread fireing events in ATL Service synchronization requirements?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 09/15/04


Date: Wed, 15 Sep 2004 09:54:06 -0700

You'll need synchronization for your list all right. The best place
would be in your global class of course. E.g. instead of using
STL list directly, wrap it in a class that exposes methods for
your objects to call and those methods provide the necessary
synchronization. This falls under my last comment - you need
proper synchronization for your data. The iteration will be a
bottleneck, however, since you need to maintain the lock around
the entire loop notifying your objects.

-- 
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Phil Ten" <pt@dafweb.com> wrote in message 
news:Or5$7gwmEHA.556@tk2msftngp13.phx.gbl...
> Sorry, I sent the previous post by mistake before finishing it.
>
> Thanks again for your post. It helps but the answer to my first
> point in my initial post is still unclear for me.
>
> I will rephrase it here. I currently maintain a list of all
> active instances of my COM object by adding the pointer
> on the object to an STL list in the constructor of the object:
>
> CDirWatch::CDirWatch()
> {
>    ClientsList.insert(this);
> }
>
> I remove the pointer from my list in the destructor of the
> COM obect:
>
> CDirWatch::~CDirWatch()
> {
>    ClientsList.erase(this);
> }
>
> My independent thread fires event using all pointers
> found in the list:
>
> CDirWatchItemList::iterator i;
> for (i=ClientsList.begin();i!=ClientsList.end();i++)
> {
>    (*i)->Fire_Notification(MyData);
> }
>
> My questions are:
>
> 1) Is the COM object Constructor/Destructor a good
> location to add/remove pointers from my global list?
>
> 2) Obviously, for each loop the object may have been
> destroyed just after extracting the pointer out of my list
> and therefore the call to "Fire_Notification" will fail
> because the pointer is no longer valid. How could I
> lock all existing COM instances while I loop though the
> list and fire events?
>
> Thanks again for all help.
>
> Phil Ten
>
>
> "Alexander Nickolov" <agnickolov@mvps.org> wrote in message
> news:O0t8JIqmEHA.1652@TK2MSFTNGP09.phx.gbl...
>> Ok, you need synchronization in the code manipulating and
>> accessing the m_vec array of sink pointers. ATL already does
>> this for you in two places - IConnectionPointImpl<> and the
>> proxy class used for firing your events. If you don't deviate
>> from these two, you don't need any extra synchronization in
>> your code as far as firing the events. Any extra synchronization
>> with regards to your object's internal state is entirely up to you.
>> I hope this answers your question.
>>
>> -- 
>> =====================================
>> Alexander Nickolov
>> Microsoft MVP [VC], MCSD
>> email: agnickolov@mvps.org
>> MVP VC FAQ: http://www.mvps.org/vcfaq
>> =====================================
>>
>> "Phil Ten" <pt@dafweb.com> wrote in message
>> news:ut7yzHpmEHA.1652@TK2MSFTNGP09.phx.gbl...
>> > Thank you for your post.
>> >
>> > I specified the free threading model for my object.
>> >
>> > I use ATL 7, I defined _ATL_FREE_THREADED in the
>> > StdAfx.h file. I checked how the code is initializing, and
>> > I could see that "CoInitializeEx(NULL, COINIT_MULTITHREADED)"
>> > is called.
>> >
>> > I hope this is the information you requested.
>> >
>> > Thanks again for all help.
>> >
>> > Phil. Ten.
>> >
>> >
>> > "Alexander Nickolov" <agnickolov@mvps.org> wrote in message
>> > news:OUQfYSomEHA.4068@tk2msftngp13.phx.gbl...
>> >> The most important question to ask first is: what is the
>> >> threading model of your object? Depending on the answer
>> >> you proceed completely differently.
>> >>
>> >> -- 
>> >> =====================================
>> >> Alexander Nickolov
>> >> Microsoft MVP [VC], MCSD
>> >> email: agnickolov@mvps.org
>> >> MVP VC FAQ: http://www.mvps.org/vcfaq
>> >> =====================================
>> >>
>> >> "Phil Ten" <pt@dafweb.com> wrote in message
>> >> news:ePmX5vjmEHA.4064@TK2MSFTNGP14.phx.gbl...
>> >> > Hello,
>> >> >
>> >> > I am writing an ATL service which fires events
>> >> > from an independent thread.
>> >> >
>> >> > My base code to fires events seem to work correctly.
>> >> > However, I still to need to implement synchronization
>> >> > features to fire events only when possible. I am
>> >> > familar with threads synchronization functions but,
>> >> > for this project (COM objects in an ATL
>> >> > service) I have no idea where I should implement
>> >> > my synchronization routines.
>> >> >
>> >> > In particular, I can see these two points:
>> >> >
>> >> > 1) Do my need to maintain my own global list of pointers
>> >> > pointing active COM clients, or is there a way
>> >> > to retreive such a list?
>> >> >
>> >> > If I must maintain my own list of pointers, where
>> >> > should I implement my code in the COM object class
>> >> > to add/remove items to my global list?
>> >> >
>> >> > 2) I assume that when the independent thread fires an event,
>> >> > I must use synchronization routines to be sure that
>> >> > the COM client will remain usable until the return of
>> >> > the fire_event function. If this is correct, where in the
>> >> > COM object classshould I implement my synchronization routines
>> >> > for this purpose?
>> >> >
>> >> > 3) Is there other pitfalls I should be aware of?
>> >> >
>> >> > Thank you in advance for all help.
>> >> >
>> >> > Phil. Ten.
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>
> 


Relevant Pages

  • Re: Global lock contention problem
    ... Access to global data is performed through global pointer. ... looking for solutions using Posix synchronization only. ... Each thread that unregisters with the polling thread shall lock the ... A registered thread acquires persistent references to an object by loading a pointer to object; dependent load barrier; hash pointer to object into index; the indexed counter in prefsis incremented. ...
    (comp.programming.threads)
  • Re: Independent thread fireing events in ATL Service synchronization requirements?
    ... and made my client list a class with synchronization inside ... my guess is that the lock for the COM object ... pointer is not enough and that I would also need to lock ... > the entire loop notifying your objects. ...
    (microsoft.public.vc.atl)
  • Re: An atomic lock-free refcounting patent application
    ... The similarities in our algorithms seem to have nothing to do with single-target synchronization. ... It only has to do with the way are are managing the nodes lifetimes. ... They make a clear separation in the paper between the single-target stuff and the method used for node lifetime tracking. ... They read the pointer along with the version count then double-check with CAS to make sure the pointer read is synchronized with the version read. ...
    (comp.programming.threads)