Skip to main content

COM Threading Models

· 3 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
7PerformanceSlowFast

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.

Unit Testing Native C++ App with out clr

· One min read

Unit Testing Native C++ Applications with out “/clr” flag or fall back to 3rd party frameworks

Visual Studio 11 provides the ability to do Unit Testing Native C++ Applications with a new C++ unit testing framework shipping with VS 11.

So the C++ developers no longer need to use the “/clr” flag or fall back to 3rd party frameworks.

To learn more about native unit testing in Visual Studio 11, please visit MSDN.

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

N-Tier Architecture

· One min read

N-Tier Architecture with ASP.NET MVC3, WCF and Entity Framework.

Advantage of using N-Tier software architecture are scalability, security, fault tolerance and etc. This article tries to introduce a decoupled, unit-testable, deployment-flexible, implementation-efficient and validation-flexible N-Tier architecture in .NET

Continue reading →

Attended Microsoft Devcon 2012

· One min read

Today (21-April-2012) I had attended the DevCon 2012 hosted at Microsoft's Gachibowli Campus.  DevCon is the event which concentrates on the latest technologies which are exclusively intended for .Net Developers.

devcon2012

The sessions that I had attended:

  1. Developing a Windows 8 Metro Style Application using HTML5 & JavaScript

    • Pratap Ladhani Architect - Microsoft
  2. SQL Server 2012 code name Denali & Programmability Enhancements

    • Sandeep Kumar Mishra MPSIT Engineering - Microsoft IT
  3. The New Age Application with HTML 5 and Kendo UI

    • Abhishek Kant Country Manager - Telerik, India
    • Svetlina Anti Software Developer - Telerik
  4. Conceptual Framework of Data Binding in XAML

    • Miroslav Nedyalkov Senior Software Developer - Telerik

Interacted with: Harish Ranganthan who is a 'Microsft Developer Evangelist' thanks to Harish as he given me a chance to explore his Windows 8 slate

  • Abhijit Jana Microsoft Associate Consultant

  • Shudhakar Microsoft

  • Shravan Kumar Kasagoni Microsoft MVP

Part-1: C++, C#, Java Syntax Differences

· One min read

C++ Main Function

  1. Main function with out any arguments
void main() { 
cout<<"Hello World !!!";
}
  1. Main function with commandline Arguments
int main(int argc, char* argv[]) {
cout<<"Hello World !!!";
return 1;
}

C# Main Function

  1. Main function with out any arguments
using System; 
namespace CSharpSample {
class Program {
static void Main() {
Console.WriteLine("Hello World !!!");
}
}
}
  1. Main Function with commandline Arguments
 using System; 
namespace CSharpSample {
class Program {
static int Main(string[] args) {
Console.WriteLine("Hello World !!!");
return 1;
}
}
}

Java Main function

  1. Main function with out any arguments Not possible Compilation error java.lang.NoSuchMethodError: main Exception in thread "main" Main function with commandline Arguments

  2. Main Function with commandline Arguments

public class Program { 
public static void main(String args[]) {
System.out.println( "Hello, World !!!" );
}
}

Part-2: C++, C#, Java Syntax Differences

· 2 min read

[Part-2: Basic Input/Output] C++, C#, Java Syntax Differences

Basic Input/Output

Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

1. C++

  • Input    - cin>>
  • Output - cout<<
// Print Greetings Program
// Basic Input/Output
void PrintGreeting(char name[])
{
cout<<"Hello " << name <<endl;
}

int main(int argc, char* argv[])
{
char name[100\];

cout<<"Enter Your Name:"; //cout -> Standard Output
cin>>name; //cin -> Standard Input

PrintGreeting(name);

return 1;
}

/* OUT PUT
Enter Your Name:nagvbt
Hello nagvbt
*/

2. C#

  • Input  - Console.WriteLine()
  • Output - Console.ReadLine()
// Print Greetings Program
// Basic Input/Output
namespace CSharpSample
{
class Program
{
static void PrintGreeting(char[] name)
{
Console.WriteLine("Hello "+ new string(name));
}

static int Main(string[] args)
{
char[] name;

Console.WriteLine("Enter Your Name:"); //Console.WriteLine() -> Standard Output
string sName = Console.ReadLine(); //Console.ReadLine() -> Standard Input
name = sName.ToCharArray();

PrintGreeting(name);

return 1;
}
}
}

/* * OUTPUT * *
Enter Your Name:
NBT
Hello NBT
* * * * * * * */

3. Java

Input - System.out.println()

Output - System.in.read()

import java.io.IOException;

class Program
{
public static void PrintGreeting(char[] name)
{
System.out.println("Hello "+ new String(name));
}

public static void main(String[] args)
{
char[] name;

System.out.print("Enter Your Name:"); //System.out.println -> Standard Output

String sName = "";
int tmp;
try
{
while((tmp = System.in.read ()) != 'n') // System.in.read -> Standard Input
{
char c = (char) tmp;
sName = sName + c;
}

}
catch (IOException e)
{
e.printStackTrace();
}

name = sName.toCharArray();
PrintGreeting(name);
}
}

/* OUTPUT
Enter Your Name:NBT
Hello NBT
*/

Part-3: C++, C#, Java Syntax Differences

· One min read

Comments, Methods, Class, objects Syntax Differences

Comments Same for C++, C#, Java

  1. Single line comments  - //
  2. Multi line comments

/ This is a a multiline comment /

Method/Function declarations

Same, except that in C# and in Java, function must always be part of a class, and must prefix with access specifier - public/private/protected

check main function for reference

Class declarations

Same but c# and Java does not require a semicolon after closing bracket '}'

_ C++ _

class myMath {
//Methods
public: int Add(int i, int j)
{
return i + j;
}
};

_ C#/Java _

 class myMath {
//Methods
public int Add(int i, int j)
{
return i + j;
}
}

Object declaration/creation C++

Object creation on Stack

myMath obj; //on stack
int result = obj.Add(1,2);

Object creation on Heap

myMath \*pobj = new myMath(); //on heap
int result = obj->Add(1,2);
delete pobj;

Java/C#

myMath obj = new myMath();
int result = obj.Add(1,2);

'Factory Method' Design Pattern using simple program

· One min read

Definition:

Creates an instance of several derived classes. or Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Program:

#include "iostream"
using namespace std;

class Product
{
public:
virtual void Show() = 0;
};

class ConcreteProductA : public Product
{
public:
virtual void Show()
{
cout<<"ConcreteProductA"<<endl;
}
};

class ConcreteProductB : public Product
{
public:
virtual void Show()
{
cout<<"ConcreteProductB"<<endl;
}
};

class Creator
{
public:
virtual Product* FactoryMethod() = 0;
};

class ConcreteCreatorA : public Creator
{
public:
ConcreteCreatorA() {}
virtual Product* FactoryMethod()
{
return new ConcreteProductA();
}
};

class ConcreteCreatorB : public Creator
{
public:
virtual Product* FactoryMethod()
{
return new ConcreteProductB();
}
};

void main()
{
Creator* creators[2];

creators[0] = new ConcreteCreatorA();
creators[1] = new ConcreteCreatorB();

for (int i=0; i < 2; i++) { Product* product = creators[i]->FactoryMethod();
cout<<"Created "<<Show();
}

getchar();
}

/*
OUT PUT
-------
Created
ConcreteProductA
Created
ConcreteProductB
*/