Skip to main content

3 posts tagged with "COM"

View All Tags

COM Threading Models

· 2 min read

COM Threading Models / Apartment Model

Definition: Multithreading in COM is referred to as the apartment model in COM

Apartment

The COM apartment is a conceptual entity that allows us to think about components and their clients in a logical way

  • An apartment is not a thread, but a thread belongs to only one apartment.

  • An apartment is not an instance of a COM object, but each COM instance belongs to only one apartment.

  • A process can have one or more apartments, depending on its implementation.

  • Apartments are created or entered by calling the CoInitialize or CoInitializeEx function. Each thread that calls CoInitialize(0) or CoInitializeEx( 0, COINIT_APARTMENTTHREADED )

  1. STA (Single Threaded Apartment) : Only One thread can join this Apartment.
  2. MTA (MultiThreaded Apartment): Multiple threads can join this Apartment.

Differences between STA and MTA

NoFeatureSTAMTA
1Synchronization provided by COMYESNO
2Uses Windows message queuesYESNO
3Can have multiple threads in an apartmentNOYES
4Must marshal interface pointers between the threads in the same apartmentN/AYES
5Must marshal interface pointers between apartmentsYESYES
6Must call Coinitialize() in every thread that uses a COM ServiceYESYES
7PerfomanceSlowFast

NOTE: ATL object wizard allows you to set the threading model. The values can be

Threading ModelDescription
Single / No ValueObject knows nothing about threads
ApartmentSTA
FreeMTA
BothBoth STA & MTA

COM Threading Model vs Win32/MFC Threads

· 2 min read

1. Win32/MFC Threads

There are two types of win32/MFC threads.

  1. User-interface thread: these types of threads are associated with one or more windows. These threads have message loops that keep the window alive and responsive to the users input.

  2. Worker thread: these threads are associated with background processing and are not associated with a window. These types oh threads does not use message loops.

NOTE: A single process can have multiple user interface threads, multiple worker threads.

2. COM Threads

COM uses same type of threads with different names.

  1. Apartment threads (User Interface Thread): This thread owns the component it creates. COM synchronizes, all calls to the component. The component does not need to be threading safe. COM does all of the marshaling and synchronization.

  2. Free threads (Worker Thread): COM does not synchronize the calls. Ant thread can access the component. These are free to use. The component must be threading safe. Marshalling is not necessary and component’s job to synchronize.

NOTE: One process can have single apartment or multiple apartments. In-proc server is example for single process with different apartments (server apartment, client apartment both are in same exe).

Out of-proc server is example for single process with single thread.

COM Containment and Aggregation

· One min read

Containment and aggregation are techniques in which one component uses another component. Those two components are outer component, inner component. Outer component contains the inner component.

ContainmentAggregation
Outer component re implement the interface say IY of inner component by forwarding calls to the inner component.Outer component will not re implement the interface say IY of inner component. Instead the outer component passes the inner component interface pointer say IY directly to the client.
Outer component is client to inner componentInner component will be directly used by the client