Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

apartment-model

0 views
Skip to first unread message

atl

unread,
Dec 24, 2001, 8:26:18 PM12/24/01
to
hi.
what does apartment mean, a piece of memory as part of process memory space?


Egbert Nierop (MVP for IIS)

unread,
Dec 24, 2001, 1:10:23 PM12/24/01
to
it means one thread initilized with CoInitilize()
when the thread ends CoUnitialize is called.
An Apartment is seen as a separated protected COM thread space that runs as
a part of the process.
When one apartment 'wants' to get info from another apartment, (pointer,
variables, interfaces) marshaling must be applied.

--
Egbert Nierop

Session management for webfarms:
http://www.nieropwebconsult.nl/asp_session_manager.htm

"atl" <only...@pub.dgnet.gd.cn> wrote in message
news:#xwDd0FjBHA.2688@tkmsftngp04...

Arkady Frankel

unread,
Dec 25, 2001, 3:50:56 AM12/25/01
to
You are atl and don't know !? :)
As behide the curtain it's window procedure of hided window COM create for
you. When you call method on apartment model object the COM transform it
to Message and send the message to this hided window which callback transfort
the message to call and call the object method function
HTH
Arkady

"atl" <only...@pub.dgnet.gd.cn> wrote in message news:<#xwDd0FjBHA.2688@tkmsftngp04>...

atl

unread,
Dec 25, 2001, 10:04:31 PM12/25/01
to
every thread in com object has an apartment?


Egbert Nierop (MVP for IIS)

unread,
Dec 25, 2001, 10:57:53 AM12/25/01
to
an apartment is a non-existing thing. It's just a concept or way of keeping
the programming rules if you are programmer..

http://msdn.microsoft.com/library/en-us/cossdk/htm/pgservices_synchronizatio
n_8703.asp?frame=true
tutorial on apartment programming
http://msdn.microsoft.com/library/en-us/com/aptclien.asp?frame=true

--
Egbert Nierop

"atl" <only...@pub.dgnet.gd.cn> wrote in message
news:#Xvc$PTjBHA.2168@tkmsftngp05...

Arkady Frankel

unread,
Dec 25, 2001, 11:12:47 AM12/25/01
to
"atl" <only...@pub.dgnet.gd.cn> wrote in message news:<#Xvc$PTjBHA.2168@tkmsftngp05>...

> every thread in com object has an apartment?

V.v. - every object live in apartment if it Initialize as apartment model.
As I can understand you the question is if
thread which produced in some method live in this apartment , the answer is
no
Arkady

Igor Tandetnik

unread,
Dec 26, 2001, 11:14:23 AM12/26/01
to
See KB Article Q150777 "INFO: Descriptions and Workings of OLE Threading
Models"
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:#xwDd0FjBHA.2688@tkmsftngp04...

atl

unread,
Dec 31, 2001, 9:35:19 PM12/31/01
to
hi. igor
can u tell me what i can do to that "apartment".
eg. split it? combine two of them or so?

Igor Tandetnik

unread,
Dec 31, 2001, 11:32:36 AM12/31/01
to
You cannot do anything to an apartment. "Apartment" is a concept - there
is no "apartment handle" or anything. A thread can join an STA or MTA
apartment by calling CoInitialize(Ex), or it can leave an apartment by
calling CoUninitialize. Two threads belonging to the same apartment can
share interface pointers directly, two threads belonging to different
apartments need to marshal interface pointers if they want to pass them
one to another.

Those are about the only operations that can be thought of as being
performed on an apartment: thread joining or leaving one, and thread
belonging to one apartment marshaling an interface pointer so it can be
unmarshaled and used in a thread belonging to another apartment.


--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:e87B7aekBHA.4792@tkmsftngp04...

atl

unread,
Jan 1, 2002, 1:13:24 PM1/1/02
to
hi. igor
happy new year.
how could u figure out & enum those apmartments?


Igor Tandetnik

unread,
Jan 2, 2002, 9:19:01 AM1/2/02
to
You cannot. Why would you want to? What exactly are you trying to do?

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:OHiOFnmkBHA.1684@tkmsftngp03...

atl

unread,
Jan 3, 2002, 2:33:06 AM1/3/02
to
hi. igor:-)
for this simple problem, is there a solution that is complex,
black-boxed,and Right by microsoft?
"Igor Tandetnik" <itand...@whenu.com> 写入消息新闻
:ezL#sh5kBHA.2204@tkmsftngp07...

Igor Tandetnik

unread,
Jan 2, 2002, 11:55:39 AM1/2/02
to
For _which_ simple problem? Many people come up with a notion that to do
A one has to do B, and then go to the newsgroup and ask "How do I do
B?". In this case, you cannot, so tell us what your problem A is, and we
may suggest an alternate route.

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:OoS3kK6kBHA.1684@tkmsftngp03...

atl

unread,
Jan 3, 2002, 1:37:04 PM1/3/02
to
hi. igor

problem A: what does apartment mean, a piece of memory as part of process
memory space?
problem B: how could u figure out & enum those apmartments even orally?

problem C: i just want to implement a com class not by atl. can i do that?

Igor Tandetnik

unread,
Jan 3, 2002, 10:46:42 AM1/3/02
to
A: Apartment is not a physical object, it does not occupy memory, there
is no "apartment handle" or something you can pass to API to perform an
operation on an apartment. Being in an apartment is a property of the
thread and of a COM object. The point of the exercise is this: COM
ensures that methods of a COM object belonging to apartment X are only
ever called on the thread belonging to the same apartment X.

A thread can belong to none or one apartment. STA (single-threaded
apartment) can have exactly one thread. MTA (multi-threaded apartment)
can have one or more threads. There may be many STAs in a process, but
no more than one MTA.

Thus, a COM object belonging to an STA is never called concurrently,
since is can only be called by the thread belonging to the same STA, and
there cannot be more than one of those. So STA object does not have to
worry about protecting data against concurrent access, and can safely
use thread-affine APIs, such as GUI. A COM object belonging to MTA can
be called concurrently, and must protect its data accordingly.

B: When a thread calls CoInitialize or
CoInitializeEx(COINIT_APARTMENTTHREADED), it is considered to belong to
an STA. Since STA can have only one thread, every STA in the process is
distinct, so there are as many STAs as there are running threads that
have called one of those init functions and have not yet called
CoUninitialize. When such a thread calls CoUninitialize, it no longer
belongs to its STA. Its STA is no more.

When a thread calls CoInitializeEx(COINIT_MULTITHREADED), it is
considered to belong to an MTA. All threads that do this enter the same
MTA, so there can be only one MTA per process. A thread calls
CoUnitialize to leave MTA. I'm not sure when MTA is destroyed: either
when the last thread leaves it, or when the thread that was first to
enter MTA (and thus essentially created it) leaves it. Maybe Alexander
Nickolov will help me here.

So if you know the threads your program creates, you can figure out the
apartments those threads enter.

C: Sure you can, though it's a lot of pain. "Inside OLE" by Kraig
Brockshmidt, available online on MSDN under Books, walks through the
procedure.


--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:ONhCl9$kBHA.2472@tkmsftngp04...

Alexander Nickolov

unread,
Jan 3, 2002, 4:40:57 PM1/3/02
to
The MTA is created when a thread in the process enters it. It is
destroyed when the last client thread leaves it. It can be recreated
again later, but it is considered to be another apartment (e.g. all
outstanding references to objects in the old MTA are invalidated
when it is closed and do not become valid in the new MTA, which
in technical terms means that all stubs are unloaded when you
close the MTA). MTA also contains threads that your code did
not create (all threads from the RPC thread pool belong there).
They do not count for closing the MTA. In an out-of-proc (local)
free-threaded server all incoming method calls are serviced on
the MTA threads from the RPC thread pool.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@geocities.com
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:e1hXY3GlBHA.2104@tkmsftngp07...

atl

unread,
Jan 5, 2002, 12:43:27 AM1/5/02
to
thanks a lot.
when i create an instance of a com object which belongs to MTA.
who will create those threads for me or i should do it by myself with
_beginthreadex or so?
or i should create those threads then create instances of com object in
those threads?


Igor Tandetnik

unread,
Jan 4, 2002, 10:04:36 AM1/4/02
to
COM takes care of mismatched threading models, creating new threads as
necessary. When STA thread creates MTA component, the component is
actually created in any MTA thread (COM can create and manage a pool of
those) and the STA thread gets a proxy. When an MTA thread creates an
STA component, the component is created on a so-called main thread,
which is a thread that was first to enter STA (if there's none, COM
creates one STA thread which becomes main thread). Again, an MTA thread
gets a proxy.

Unfortunately, COM uses CreateThread and not _beginthread(ex) to create
new threads, which may cause leaks as per KB Article Q104641 "INFO:
Using C Run-Time Functions and CreateThread()", especially for MTA
objects where threads come and go as they please. I have once seen a
complete list of RTL functions that should be avoided by MTA components,
but unfortunately I seem to have misplaced it. Note that the article
does not apply if you use RTL DLL - that one initializes its per-thread
data in DllMain(DLL_THREAD_ATTACH).


--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:e8rbjWSlBHA.1592@tkmsftngp02...

atl

unread,
Jan 5, 2002, 2:25:14 AM1/5/02
to
concretely, supposed i want to create a winsock comp which is ICQism to
search the web.
how should i do that:
implement a interface(including a lot of method such as connect, getsth.
close)
then in the client side. to create instances of this comp.

or should i do that:
in the above interface methods, find a entry point to createthread,then
connect,
getsth,close, and use all of the syn features to synchornize all of this
threads.


Igor Tandetnik

unread,
Jan 4, 2002, 10:29:32 AM1/4/02
to
I don't understand your question, sorry.

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:ulFedPTlBHA.1684@tkmsftngp03...

atl

unread,
Jan 5, 2002, 2:44:36 AM1/5/02
to
now i found that hacker is not welcome in the world:-)
i just want to know a little of their programming concept.
maybe i need more readings about com-threading.


atl

unread,
Jan 5, 2002, 2:48:55 PM1/5/02
to
hi, igor:-)

i should say my question is (conceptionally):
how to divide the labor of threading-related & com-related working
in a performance-critical sulotion.
thanks.


Igor Tandetnik

unread,
Jan 7, 2002, 9:52:29 AM1/7/02
to
Now that's a very open-ended question. I doubt it can be answered
generally. Not by me, at least. I'm more of a client-side,
user-interface, muck-with-IE kind of guy. Beg Alexander, he's our
server-side guru 'round here. Still, I don't think you'll get far
without a somewhat more concrete problem.

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"atl" <only...@pub.dgnet.gd.cn> wrote in message

news:#SEYovZlBHA.956@tkmsftngp05...

Alexander Nickolov

unread,
Jan 7, 2002, 3:14:15 PM1/7/02
to
Well, a generic question deserves generic answer - keep your thread
maintenance to a minimum to conserver the CPU for the productive
code. I know this won't help you much, so as Igor said already - define
your problem in more specific terms. Each problem has its unique
solution - no one size fits all solutions...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@geocities.com
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

"atl" <only...@pub.dgnet.gd.cn> wrote in message news:#SEYovZlBHA.956@tkmsftngp05...

atl

unread,
Jan 8, 2002, 12:48:20 PM1/8/02
to
i am learning com concept. i would like to all of com things really totally
transparent to us.
"Alexander Nickolov" <agnic...@geocities.com>
??????:erobld7lBHA.2752@tkmsftngp02...
0 new messages