Library Zone Articles
External Articles
Byte Size

Discovery Zone Catalogue
Diary
Links
Bookstore
Interactive Zone Ask the Gurus
Discussion Groups
Newsletters
Feedback
Etc Cartoons
Humour
COMpetition
Advertising
Site Builder ASP Web Ring ASP Web Ring

Click here
The Developer's Resource & Community Site
COM XML ASP Java & Misc. NEW: VS.NET
International This Week Forums Author Central Find a Job

COM Threading Models

Article ammended after feedback by Julian Templeman, thanks to Julian.

Introduction

COM uses the same threading mechanism that Win32 uses. It uses the Win32 API for creating and synchronizing threads. However, threading models are referred to differently in Win32 and COM.

The Win32 Threading Model

Win32 defines two types of threads. User-Interface threads and Worker threads. Each process in Win32 can have one or more User Interface and/or multiple Worker threads. User Interface threads have message-loops that receive events targetted to that window and are hence associated with one or more windows. Worker threads are used for background processing and are not associated with any window.

As a developer, there is one other major difference that you have to be aware of when developing Win32 threads. User Interface threads always own one or more windows. Whenever there is an event for a particular window, the UI thread takes care of calling the appropriate method. Since the message loop is executed on the same UI thread regardless of the thread that sent the message, synchronization is guaranteed by Windows. You as the programmer need not write any special code for thread synchronization. However, if you are developing worker threads, it is your responsibility to handle thread synchronization and prevent deadlocks or racing conditions.

The COM Threading Model

COM terminology to threads is slightly different. There are three types of threading models in COM. They are apartment threading, free threading and rental threading ( introduced for MTS ) The closest analogy to Win32 is that UI threads are analogically similar to the Apartment threading model and worker threads are analogically similar to the Free threading model.

Apartment Threading Model (single threaded apartments)

This model was introduced in the first version of COM with Windows NT3.51 and later Windows 95. The apartment model consists of a multithreaded process that contains only one COM object per thread. Single Threaded Apartments (STA)- This also means that each thread can be called an apartment and each apartment is single threaded. All calls are passed through the Win32 message processing system. COM ensures that these calls are synchronized. Each thread has its own apartment or execution context and at any point in time, only one thread can access this apartment. Each thread in an apartment can receive direct calls only from a thread that belongs to that apartment. The call parametes have to be marshalled between apartments. COM handles marshalling between apartments through the Windows messaging system.

Free Threading Model (multi threaded apartments)

This model was introduced with Windows NT 4.0 and Windows95 with DCOM. The free threaded model allows multiple threads to access a single COM object. Free threaded COM objects have to ensure thread synchronization and they must implement message handlers which are thread aware and thread safe. Calls may not be passed through the Win32 messaging system nor does COM synchronize the calls, since the same method may be called from different processes simultaneously. Free threaded objects should be able to handle calls to their methods from other threads at any time and to handle calls from multiple threads simultaneously. Parameters are passed directly to any thread since all free threads reside in the same apartment. These are also called Multi-Threaded Apartments ( MTA )

Both Apartment and Free Threaded Model

It is possible for a process to have both the apartment and free threaded model. The only restriction is that you can have only one free threaded apartment but you can have multiple single threaded apartments. Interface pointers and data have to be marshalled between apartments. Calls to objects within the STAs will be synchronized by Win32 whereas calls to the MTAs will not be synchronized at all.

Thread Neutral Apartment

Components that use the Thread Neutral Apartment model (TNA), mark themselves as Free Threaded or Both. Each instance of a COM class can run on a different thread each time a method is called. When a thread is executing a method in a COM object, and that method creates a new object, MTS will suspend the current thread and create a new thread to handle the new object. Like the MTA, TNA's allow more than one thread to enter an apartment. However, once a thread has entered an apartment, it obtains an apartment-wide lock and no other thread can enter that apartment until it exits. This model was introduced into MTS to ensure that context switches are faster. The MTS COM object is however oblivious to the fact about multithreading and assumes that it is single threaded.

© Copyright 1997-2000 Gopalan Suresh Raj. Reproduced with Permission


Mail a question to the author!!

As part of the IDevResource commitment to Open Publishing, all of our authors are available to answer all of your trickiest questions at Author Central. For information about the authors, or to mail a question, visit them at Author Central.

Did you like this article?

If you liked this article, tell us about it. You can email your remarks to us at [email protected]

Have your say about the article. You can make your point about the article by mailing [email protected] (If you haven't allready joined, you can join by going to onelist.com/community/dev-com).

Want to read more articles by this author?

Try these:

Byte size articles:

COM Threading Models By Gopalan Suresh Raj, 070200
Gopalan explains the differences in COM and Win 32 threading models.
Go To Article.

ActiveX & COM By Gopalan Suresh Raj, 270100
Gopalan explains the basics of ActiveX / COM as a truly distributed Object Oriented Architecture.
Go To Article.

Full size articles:

COM Channel:

Java COM Integration - Use Visual J++ to implement COM Objects By Gopalan Suresh Raj.
Go To Article.

Developing an MSMQ Server Application using VJ++ By Gopalan Suresh Raj.
Go To Article.

Developing an MSMQ Client using VJ++ By Gopalan Suresh Raj.
Go To Article.

Coding a DCOM Server Component from IDL By Gopalan Suresh Raj.

Coding a DCOM Client By Gopalan Suresh Raj.

Microsoft Transaction Server By Gopalan Suresh Raj.
Gopalans introductory article on Microsoft Transaction Server intorduces the basics to MTS, and leads in to the example articles included in the series.

Developing a Simple MTS Server Component By Gopalan Suresh Raj.
Part 1 of a two part example.

Developing a Simple MTS Client Application By Gopalan Suresh Raj.
Part 2 of a two part example.

Developing The Bank Account IDL By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - Developing The Bank Account IDL is part 1 of a 3 part example.

MTS Server Component By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the second part of this three part example.

MTS Client By Gopalan Suresh Raj.
A Three-Tier Architecture for a Bank Checking Account - MTS Server Component is the third part of this three part example.

Java Channel:

Enterprise Java Beans By Gopalan Suresh Raj.
In this introduction to Enterprise Java Beans, Gopalan covers the bases then goes on to demonstrate how to build server side business object components. This article is the introduction to Gopalan's series of Enterprise JavaBeans articles. (This series of articles is © Copyright 1997-2000 Gopalan Suresh Raj. Reproduced with Permission)

Enterprise Java Beans Series - Components at the Server By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Model By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Naming Services and JNDI By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Transactions and JTS By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Lifecycle By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Servers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Containers By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Components By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Session Beans By Gopalan Suresh Raj.

Enterprise Java Beans Series - EJB Entity Beans By Gopalan Suresh Raj.

Enterprise Java Beans Series - Writing an Entity Bean By Gopalan Suresh Raj.
Part 1 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing a Session Bean By Gopalan Suresh Raj.
Part 2 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Client By Gopalan Suresh Raj.
Part 3 of a four part series: A four tier bank account example

Enterprise Java Beans Series - Writing an EJB Servlet Client By Gopalan Suresh Raj.
Part 4 of a four part series: A four tier bank account example

Further Reading:

  1. Microsoft Transaction Server By Richard Grimes, introduces Microsoft Transaction Server
  2. What COM is all about By Richard Grimes, an introductory article to COM, covering all basics including OLE, Activex and DLL's.
  3. String Binding Moniker By Frank Rem, shows how a SB Moniker resolves a connection with a DCE RP server running on Linux using a VB client.
  4. Inside COM Monikers By Davide Marcato, on COMdeveloper.com. monikers are still one of the most clever but at the same time most misunderstood idioms in the COM universe. Davide explains them.

More ByteSize articles:

'ILoveYou' By Richard Grimes, 200500
Richard Grimes discusses the release of the Microsoft patch for the 'ILoveYou' virus. Go To Article.

COM+ 2.0 - First Announcement of Microsoft's New Technology By Richard Grimes, 100500
Richard Grimes reports on the recent news about COM+ 2. Go To Article.

How to use DDX with WTL? By Girish Bharadwaj, 270300
Go To Article.

ATL Server By Richard Grimes, 220200
Richard's report from European Devweek 2000, and VCDC Bonn 2000 gives an insight into the new ATL Server technology.
Go To Article.

COM Threading Models By Gopalan Suresh Raj, 070200
Gopalan explains the differences in COM and Win 32 threading models.
Go To Article.

ActiveX & COM By Gopalan Suresh Raj, 270100
Gopalan explains the basics of ActiveX / COM as a truly distributed Object Oriented Architecture.
Go To Article.

Type Library Info, XML and a bit of XSL for fun! by Richard Anderson 121199
This article assumes you understand COM, Type Libraries, XML and enough about VB that you can either use the code as a starting point, or, translate it into another language.
Go To Article.
This article can be downloaded: Zip File (76kb).

COM and Apartments By Richard Grimes, 070100
A discussion on the creation and use of "Apartments" in Win32.
Go To Article.

What is WTL? By Richard Grimes, 070100
Richard gives a basic introduction to WTL.
Go To Article.

An Introduction to Interface Programming By Richard Grimes, 070100
Richard describes the background to interfaces.
Go To Article.

Some links on COM:

  1. https://www.microsoft.com/com/ Microsoft's COM pages
  2. COMdeveloper.comVisit the COMdeveloper Web site for news items, articles, master classes, book reviews, a listserv, competitions, and a newsletter on COM, DCOM, ATL, and COM+ for VB, Java, and C++ developers.
  3. Chappell & Associates Visit the site hosted by Chappell & Associates to learn more about their seminars, videos, and articles on distributed computing, objects, and more.
  4. Codemarine, Inc. Visit the CodeMarine Web site to find information about in-depth COM training, mentoring, and consulting offerings.
  5. DevelopMentor Visit the CodeMarine Web site to find information about in-depth COM training, mentoring, and consulting offerings.


Author Bio:

Author: Gopalan Suresh Raj

Gopalan Suresh Raj is a Software Architect, Developer and an active Author.

He is contributing author to a couple of books "Enterprise JavaComputing-Applications and Architecture"(Cambridge University Press, June '99) and "TheAwesome Power of JavaBeans"(Manning, July'98).

His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (https://www.execpc.com/~gopalan)or mail him at [email protected].

Go to Gopalan's pages in Author Central.


Click here

Contribute to IDR:

To contribute an article to IDR, a click here.

To contact us at IDevResource.com, use our feedback form, or email us.

To comment on the site contact our webmaster.

Promoted by CyberSavvy UK - website promotion experts

All content © Copyright 2000 IDevResource.com, Disclaimer notice

Visit the IDR Bookstore!

Visit the IDR Forums

Join the Developers Webring

Java COM integration

WTL Architecture by Richard Grimes