Re: Independent thread fireing events in ATL Service synchronization requirements?
From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 09/15/04
- Next message: Alex Blekhman: "Re: Regexes in ATL 2"
- Previous message: Igor Tandetnik: "Re: Change Interface name"
- In reply to: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Next in thread: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Reply: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Messages sorted by: [ date ] [ thread ]
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. >> >> > >> >> > >> >> > >> >> >> >> >> > >> > >> >> > >
- Next message: Alex Blekhman: "Re: Regexes in ATL 2"
- Previous message: Igor Tandetnik: "Re: Change Interface name"
- In reply to: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Next in thread: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Reply: Phil Ten: "Re: Independent thread fireing events in ATL Service synchronization requirements?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|