那下面那句话的意思------Threads live in apartments. Objects live in apartments. Sometimes an object shares the same apartment as the thread that created it, and sometimes an object lives in a different apartment than the thread that created it.COM currently defines two kinds of apartments: a single-threaded apartment (often abbreviated STA) and a multithreaded apartment (often abbreviated MTA). The names are pretty self-explanatory. A single-threaded apartment houses a single thread, and a multithreaded apartment houses multiple threads. A single process has at most one MTA, but it might have many STAs.
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // This creates a single threaded apartment(STA) CoInitializeEx(NULL, COINIT_MULTITHREADED); // This creates/enters a multi-threaded apartment(MTA)Although the COM SDK emphasizes that "apartment" is a logical concept, it actually has great impacts on the infrastructure of COM. It's not merely logically meaningful. In fact, you can think of "apartment" as a grouping of threads. Different grouping makes different apartments. Just keep these criteria in mind: One process can have only one MTA, but many STAs; An apartment(STA or MTA) can only exist in one process; If the client and the server are in different apartment (different STA, or different MTA, or one STA, the other MTA), marshalling is needed, although you may not see the marshalling yourself.
总之,套间为了解决com的多线程问题。
开个玩笑,仅供参考,高手赐教。
Objects live in apartments. Sometimes an object shares the same apartment as the thread that created it, and sometimes an object lives in a different apartment than the thread that created it.COM currently defines two kinds of apartments: a single-threaded apartment (often abbreviated STA) and a multithreaded apartment (often abbreviated MTA). The names are pretty self-explanatory. A single-threaded apartment houses a single thread, and a multithreaded apartment houses multiple threads. A single process has at most one MTA, but it might have many STAs.
// This creates a single threaded apartment(STA)
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// This creates/enters a multi-threaded apartment(MTA)Although the COM SDK emphasizes that "apartment" is a logical concept, it actually has great impacts on the infrastructure of COM. It's not merely logically meaningful.
In fact, you can think of "apartment" as a grouping of threads. Different grouping makes different apartments. Just keep these criteria in mind: One process can have only one MTA, but many STAs; An apartment(STA or MTA) can only exist in one process; If the client and the server are in different apartment (different STA, or different MTA, or one STA, the other MTA), marshalling is needed, although you may not see the marshalling yourself.