Re: COM Apartments - Message Loops



Well, one that I read I probably misread now that I look at it again.
All I need was for you to reinforce the correct idea. In my earlier
applications I always used critical sections when accessing an object
from multiple threads so I never had issues. I guess I never relied
upon COM to synchronize things for me.

But I'm still a bit confused. I understand that CoInitialize() is
called to enter into an STA. But if you then spawn a thread while in an
STA do you have to call CoInitialize() again for that spawned thread to
enter into an STA. I found that that wasn't necessary and also that it
didn't create problems since no exception where thrown upon the use of
COM in those spawned threads. The semantics of what it means to call
CoInitialize are still a bit confused for me.



Igor Tandetnik wrote:
Ron Ayoub <ronaldayoub@xxxxxxxxx> wrote:
My question is basic. Lots of articles say that when CoInitialize() is
called a thread enters an STA and a message loop is created INTERNALLY
in order to serialize calls to the objects created in that STA.

Can you cite one such article? STA apartment _requires_ a message pump,
but does not automatically _provide_ one. It is the responsibility of
the thread entering STA to run a message pump if it wants to accept
incoming cross-apartment calls.

Lots
of articles also say that you have to create and pump message
EXPLICITLY if you expect cross apartment interaction to occur. This
latter one is kind of strange to me since I've done lots of
programming of server applications that don't have any message loop
at all and they enter into an STA at the start and spawn numerous
threads and there is no problem with these numerous threads
interacting with objects created in separate threads.

These applications probably violate COM rules by calling STA objects
from worker threads without proper marshalling. Neither the compiler nor
the runtime will stop you from calling a thread-unsafe object
concurrently from multiple threads. You might even get away with it for
a while, until a race condition bites you at the most unfortunate
moment.

Is it because
all threads are in a single STA.

You do realsize that STA stands for "single-threaded apartment", right?
As in the apartment that can only ever contain one thread?

In that case, the literature that
claims one thread per STA is also wrong or misleading in some manner.

The literature is correct in this regard.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925

.



Relevant Pages

  • Re: breaking changes from ATL 7.0 to 7.1 ? Problem with GetMessage
    ... >>> So when I start a dialog in FinalConstruct, it's not in an apartment ... >> happens to join an STA apartment your COM object lives in. ... >> A window does not own threads - it's the other way round. ... >> its message pump, so it's difficult for it to call IsDialogMessage ...
    (microsoft.public.vc.atl)
  • Re: difference between STA and MTA
    ... There's never been a version of COM for 16 bit windows. ... > call the method CoInitialize(). ... > Apartment (STA). ... In that apartment there can live only one thread. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: out-of-process servers -- how do I force thread separation?
    ... their own apartment... ... pool of STA threads. ... If the accessor object is STA, that plus an out-of-process server (all ... hardware resouce & data, gets me 95% there. ...
    (microsoft.public.vc.atl)
  • Re: Apartments and threading model
    ... An apartment is a COM abstraction about processes ... An STA is bound to a single thread and is defined as the ... CoInitialize - this is destroyed by the matching call to ... The MTA has very different rules. ...
    (microsoft.public.vc.atl)
  • Re: PostMessage not posting with CWindowImpl
    ... >> Is your object's threading model Apartment? ... the host is required by COM specification to run a message pump. ... either STA or MTA. ...
    (microsoft.public.vc.atl)