Re: COM Apartments - Message Loops
- From: "Ron Ayoub" <ronaldayoub@xxxxxxxxx>
- Date: 25 Oct 2006 12:57:55 -0700
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
.
- Follow-Ups:
- Re: COM Apartments - Message Loops
- From: Igor Tandetnik
- Re: COM Apartments - Message Loops
- References:
- COM Apartments - Message Loops
- From: Ron Ayoub
- Re: COM Apartments - Message Loops
- From: Igor Tandetnik
- COM Apartments - Message Loops
- Prev by Date: Re: COM Apartments - Message Loops
- Next by Date: Re: COM Apartments - Message Loops
- Previous by thread: Re: COM Apartments - Message Loops
- Next by thread: Re: COM Apartments - Message Loops
- Index(es):
Relevant Pages
|
|