[omniORB] RE: omniORB-list digest, Vol 1 #245 - 9 msgs

JHJE (Jan Holst Jensen) jhje@novonordisk.com
Tue Dec 17 08:55:02 2002


> Subject: RE: [omniORB] Single Threaded Policy: really same thread?
> Date: Mon, 16 Dec 2002 17:55:26 -0700
> From: "Kevin Wooten" <kwooten@itracs.com>
...
> I am very familiar with COM threading; this scheme of using the same
> exact thread is exactly what COM does for all single threaded
> components. Basically whatever thread creates a COM component 

Yes, and that would exactly be one reason for wanting to use the same thread
for all requests. If you make use of a COM server to complete the request
(as I do in some of my CORBA servers - effectively wrapping a COM API) it
would be very convenient if the ORB could guarantee that the thread that
instantiated the COM object would also be the one doing operations on it.
Otherwise, in order for the request not to fail, you would have to make sure
that each thread has called CoInitialize before invoking any methods on the
COM server. No big deal, but still some bookkeeping to do.

> ALL components in COM?? MS's own documentation talks about access to
> global data (basically the example I gave), but this can be 
> solved by a
> simple mutex! I have looked everywhere for this answer and cannot find

Well, the apartment model is used for all components in COM, but not all
apartments involve forcing a thread switch. If your COM server lives in a
"free-threaded" apartment, any thread is free to call it's methods directly.
It is then the responsibility of the COM server to protect it's internal
state or global data from concurrent access. So, if you are in control of
the COM server code, you can simple make sure it is thread-safe and
configure it to run in an apartment with multiple threads. Actually, I think
you can hack the registry to make any COM server run in a "free-threaded"
apartment, but if your server is not thread-safe be prepared for trouble...

Many third-party COM servers I've encountered like to run in a
single-threaded apartment. When you don't have to think about thread-safety,
it simply makes it easier to program the COM server. You'd rather have
something that is a bit slower and works than something fast that crashes
once in a while. So, I think the rationale has been to shield entry-level
programmers from threading issues (actually a wise decision IMHO), with the
downside of introducing the confusing concept of "apartments". Now, if they
had only been named in a more logical way :-)...

Cheers

-- Jan Holst Jensen, Novo Nordisk A/S, Denmark