Home arrow C++ arrow Page 2 - COM Threading Models Explained

COM Threading Models Explained

Still confused about COM? In this article Neville continues his series on COM and explains the various threading models available to us when creating COM components.

Author Info:
By: Neville Mehta
Rating: 3 stars3 stars3 stars3 stars3 stars / 26
August 08, 2002
  1. · COM Threading Models Explained
  2. · What Is A Thread?
  3. · Conclusion

print this article

COM Threading Models Explained - What Is A Thread?
(Page 2 of 3 )

Before we proceed, let me tell you what threads are. Every application when started on your operating system is a process. Now, that process can have many threads being executed under it. Let me give you an example. Say you start Microsoft Word on your computer. You have just started a process. After starting it you just gave the print command for a few pages. That's a thread under that process. While the pages print, you start browsing the clipart wizard in search of some clip art images. That's another thread under the same process.

Threads can also have priorities. A thread may be of high priority or low priority. A thread with high priority may interrupt a thread with low priority. Applications can be single threaded or multi threaded.

Every process on a computer can have more than one task or thread running under it. This wasn't true in the "olden days". Applications used to be single threaded during the Windows 3.1 days. This meant that only one task could be performed at one time, and that all other tasks would have to wait until that task finished executing.

Many a time, large applications would just make the CPU freeze or make you wait for a long period of time until their task finished executing. Microsoft then introduced multi threading in Windows 95 and Windows NT. You could now perform more than one task at once without waiting for the other tasks to complete first.

Let's take a look at the threading models in COM.

The STA (Single Threaded Apartment) Model
The first version of COM supported something called the STA (Single Threaded Apartment) model. In this model COM allowed us to have just one thread in an apartment, however it also allowed us to have multiple apartments under one process.

It allowed clients to create multiple apartments. One thread was created in an apartment. All the data, which was transferred from one apartment to another, was marshaled through a proxy. In this threading model, COM serialized execution of threads through a message queue.

The order of execution of threads was picked up serially from the queue so that there was only one active call made to the client at one time. This meant that users could issue multiple commands (or threads) at the same time, which would all be assembled serially in a queue.

However, only one command (or thread) would be executed at any one time. They would be executed in the order that they were issued. So, the STA (Single Threaded Apartment) model made a users life a lot easier by letting that user issue more than one command at a time.

The Single Threading Model

The MTA (Multi Threaded Apartment) Model
Windows NT introduced multi-threading. They introduced a new threading model, which was called the MTA (Multi-Threaded Apartment) model. Even COM supported this model. In the MTA (Multi-Threaded Apartment) model, COM does not synchronize the execution of the threads. You as a developer have to provide the synchronization code for you component. In this model only one apartment is created. However, that one component can hold any number of threads in it (unlike the STA (Single-Threaded Apartment) model which had any number of apartments but could hold only one thread per apartment).

This model was a lot more advanced than the STA (Single-Threaded Apartment) model. Whenever these threads (which were part of one apartment), needed to share data, the data didn't have to be marshaled via a proxy, as all of the threads were part of one apartment itself. No message queue was maintained by COM.

Execution was a lot faster, however it was the developer's responsibility for ensuring that two threads do not have access to the same COM object at one time. All of the synchronization code had to be programmed by the developer himself and maybe that was the only drawback of the MTA (Multi-Threaded Apartment) model.

The MTA model provides us developers with a lot of flexibility, but at the same time gives us a lot of responsibility too, as inefficient thread synchronization code would really affect the performance of the application. An efficient coder could do wonders to the application performance if using the MTA model instead of the STA model.

The Multi Threading Model

The Mixed Threading Model
There was another threading model introduced in COM called the Mixed-Threading model. This model was just a combination of the STA (Single-Threaded Apartment) model and the MTA (Multi-Threaded Apartment) model. You could have any number of single threaded apartments storing one thread in them. You could also simultaneously have a multi-threading apartment storing any number of threads in it. It solely depended upon the coder as to which threading model was used when coding a COM component.

The Mixed Threading Model

The ATL COM object wizard supports the following threading options:
  • Single: Similar to single threading, which existed during the Windows 3.1 days. It permitted creation of only one thread at a time.
  • Apartment: Similar to the STA (Single-Threaded Apartment) model.
  • Both: Similar to the Mixed-Threading model.
  • Free: Similar to the MTA (Multi-Threaded Apartment) model.
COM currently supports the 4 threading models shown above.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials