COM线程模型 - MTA接口 - Part IV - (运行线程)

From : http://blog.csdn.net/zj510/article/details/39053647

 

一个STA对象只能属于一个STA套间,那么一个STA对象一定是在一个线程里面运行的。所以STA对象不需要考虑并发,因为它永远是串行运行的。

那么一个MTA对象在哪个线程里面运行的?


MTA套间调用MTA对象

看下面这段代码:

  1. // TestCom.cpp : Defines the entry point for the console application. 
  2. // 
  3.  
  4. #include "stdafx.h" 
  5. #include <atlbase.h> 
  6. #include <atlcom.h> 
  7. #include "../MyCOM/MyCOM_i.h" 
  8. #include "../MyCOM/MyCOM_i.c" 
  9.  
  10. #include <iostream> 
  11. #include <thread> 
  12.  
  13.  
  14. using namespace std; 
  15.  
  16. void WorkThread() 
  17.     CoInitializeEx(0, COINIT_MULTITHREADED); 
  18.  
  19.     WCHAR temp[100] = { 0 }; 
  20.     swprintf_s(temp, L"thread tid: %d\n", ::GetCurrentThreadId()); 
  21.     ::OutputDebugStringW(temp); 
  22.  
  23.     { 
  24.         CComPtr<IMyRect> spRect; 
  25.         HRESULT hr = spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER); 
  26.  
  27.         hr = spRect->Draw(CComBSTR(L"red")); 
  28.     } 
  29.  
  30.     CoUninitialize(); 
  31.  
  32. int _tmain(int argc, _TCHAR* argv[]) 
  33.     CoInitializeEx(0, COINIT_MULTITHREADED); 
  34.  
  35.     WCHAR temp[100] = {0}; 
  36.     swprintf_s(temp, L"main tid: %d\n", ::GetCurrentThreadId()); 
  37.     ::OutputDebugStringW(temp); 
  38.  
  39.     { 
  40.         CComPtr<IMyRect> spRect; 
  41.         spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER); 
  42.  
  43.         spRect->Draw(CComBSTR(L"red")); 
  44.  
  45.         CComPtr<IMyRect> spRect2; 
  46. spRect2.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);
  47.  
  48.         spRect2->Draw(CComBSTR(L"blue")); 
  49.     } 
  50.      
  51.     std::thread t(WorkThread); 
  52.     std::thread t1(WorkThread); 
  53.     std::thread t2(WorkThread); 
  54.  
  55.     CoUninitialize(); 
  56.  
  57.     t.join(); 
  58.     t1.join(); 
  59.     t2.join(); 
  60.  
  61.     return 0; 
// TestCom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <atlbase.h>
#include <atlcom.h>
#include "../MyCOM/MyCOM_i.h"
#include "../MyCOM/MyCOM_i.c"

#include <iostream>
#include <thread>


using namespace std;

void WorkThread()
{
    CoInitializeEx(0, COINIT_MULTITHREADED);

    WCHAR temp[100] = { 0 };
    swprintf_s(temp, L"thread tid: %d\n", ::GetCurrentThreadId());
    ::OutputDebugStringW(temp);

    {
        CComPtr<IMyRect> spRect;
        HRESULT hr = spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        hr = spRect->Draw(CComBSTR(L"red"));
    }

    CoUninitialize();
}

int _tmain(int argc, _TCHAR* argv[])
{
	CoInitializeEx(0, COINIT_MULTITHREADED);

    WCHAR temp[100] = {0};
    swprintf_s(temp, L"main tid: %d\n", ::GetCurrentThreadId());
    ::OutputDebugStringW(temp);

    {
        CComPtr<IMyRect> spRect;
        spRect.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        spRect->Draw(CComBSTR(L"red"));

        CComPtr<IMyRect> spRect2;
        spRect2.CoCreateInstance(CLSID_MyRect, NULL, CLSCTX_INPROC_SERVER);

        spRect2->Draw(CComBSTR(L"blue"));
    }
	
    std::thread t(WorkThread);
    std::thread t1(WorkThread);
    std::thread t2(WorkThread);

	CoUninitialize();

    t.join();
    t1.join();
    t2.join();

	return 0;
}

主线程初始化MTA套间,辅助线程也是。


COM对象都是运行在创建它的线程里面。


STA套间调用MTA对象

MTA对象是运行在系统创建的线程里面,看前面一篇文章。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值