致开

Apartments in COM

A previous article gives a simple introduction to Single Thread Apartment, also known as STA. This article is going to talk the other two threading models, Multiple Thread Apartment (MTA) and Neutral Apartment.

As we said, if one COM Object lives in the STA, then calls to the object, from its living apartment or other apartments, will be serialized. This can help you to solve the Concurrency hell. Besides, if other Apartments make calls to the STA object, there needs the proxy and the stub, and you need to marshal the interface pointer. The called thread will be held, giving control to the STA thread. This will lead to the Thread Switch, which hurts the performance a bit, including the user-mode and kernel-mode switches.

So you may want to control the Concurrency by yourself. Here the MTA comes. In MTA apartment, COM will not do the serialization for your COM objects, leaving all stuffs for you. All calls made to the MTA objects are directly called, which means at one time there may be several calls happening on one MTA object, if you don't do any threaad synchronization. This may break the data consistentency. But this is also flexiable.
Here is some points about the MTA:
 - One process can only contain one MTA apartment.
 - One MTA aparatment can contain more than one threads.
 - COM objects labed as "Free" will be created in the MTA apartment.
 - Thread calling the CoInitializeEx(null, COINIT_MULTITHREADED) will join the MTA apartment.
 - You need to marshal the interface pointers if you want to pass the MTA object interface pointers to other apartments.

The above is all about the MTA apartment.

OK, let's say a bit about the drawbacks of both the STA and MTA apartments.
One of the things we care most is the performance. From the above description, you can see that if calls made between the apartments will via the stub and proxy, and the Thread Switch. No matter you made calls from the STA to MTA, or MTA to STA, this is going to happe. And thread switch is a bit burden for the system. So why don't we do better?

Here comes the Neutral apartment.
Neutral apartment is a concept with the introduction of COM+, and is supported after windows 2000. Undoubtly, our machine versions are newer that windows 2000. So don't worry about whether it is supported in your computer or not.
What benifit can Neutral apartment bring to us?
Say an object living in the Neutral apartment. And you want to make a call to it from the STA apartment. OK. Here is how it works. Your STA thread will continue to run after making calls to the Neutral apartment, giving no control to anybody else. Neutral apartment finds your call coming, and instead of holding your thread, it did a little about the COM object context, and then let your thread continue running.
See, this avoids the thread switch!

Neutral Apartment doesn't contain any thread, which means that you could let your thread join in this apartment. Only objects live inside. You can mark your COM object as "Neutral" when you implements the COM object.

OK, the above is all I want to say. If you want more, go deeply in books!

posted on 2008-12-16 21:55 Xiaxi 阅读(1253) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜