COM线程模型 - MTA接口 - Part I

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

 

除了STA之外,COM组件的另外一种非常重要的模型就是MTA.

有关MTA,先阅读MSDN, http://msdn.microsoft.com/en-us/library/windows/desktop/ms693421(v=vs.85).aspx

需要仔细阅读。罗列几个重要的规则:

  1. • COM provides call synchronization for single-threaded apartments only.  
  2. • Multithreaded apartments do not receive calls while making calls (on the same thread).  
  3. • Multithreaded apartments cannot make input-synchronized calls.  
  4. • Asynchronous calls are converted to synchronous calls in multithreaded apartments.  
  5. • The message filter is not called for any thread in a multithreaded apartment.  
• COM provides call synchronization for single-threaded apartments only. 
• Multithreaded apartments do not receive calls while making calls (on the same thread). 
• Multithreaded apartments cannot make input-synchronized calls. 
• Asynchronous calls are converted to synchronous calls in multithreaded apartments. 
• The message filter is not called for any thread in a multithreaded apartment. 

我觉得其他有两个比较重要,跟STA相比,

1. COM系统不会帮助序列号,需要程序员自己来处理并发同步问题;

2. 不需要消息循环。

另外,一个进程里面只能有一个MTA套间,MTA套间里面可以有多个线程。MSDN上解释的很清楚:

  1. In a multithreaded apartment model, all the threads in the process that have been initialized as free-threaded reside in a single apartment. Therefore, there is no need to marshal between threads. The threads need not retrieve and dispatch messages because COM does not use window messages in this model. 
In a multithreaded apartment model, all the threads in the process that have been initialized as free-threaded reside in a single apartment. Therefore, there is no need to marshal between threads. The threads need not retrieve and dispatch messages because COM does not use window messages in this model.

MTA组件

先来创建一个MTA组件吧,很简单。就是在Threading model中选择Free。如图:

这次的接口名字叫做IMyLine,同样写个简单Draw函数:

  1. STDMETHODIMP CMyLine::Draw(BSTR color) 
  2.     // TODO: Add your implementation code here 
  3.     WCHAR temp[100] = { 0 }; 
  4.     swprintf_s(temp, L"IMyLine::Draw, color: %s, tid: %d\n", color, ::GetCurrentThreadId()); 
  5.     OutputDebugStringW(temp); 
  6.  
  7.     std::this_thread::sleep_for(std::chrono::milliseconds(100)); 
  8.  
  9.     memset(temp, 0, sizeof(temp)); 
  10.     swprintf_s(temp, L"IMyLine::Draw, end, tid: %d\n", ::GetCurrentThreadId()); 
  11.     OutputDebugStringW(temp); 
  12.  
  13.     return S_OK; 
STDMETHODIMP CMyLine::Draw(BSTR color)
{
	// TODO: Add your implementation code here
	WCHAR temp[100] = { 0 };
	swprintf_s(temp, L"IMyLine::Draw, color: %s, tid: %d\n", color, ::GetCurrentThreadId());
	OutputDebugStringW(temp);

	std::this_thread::sleep_for(std::chrono::milliseconds(100));

	memset(temp, 0, sizeof(temp));
	swprintf_s(temp, L"IMyLine::Draw, end, tid: %d\n", ::GetCurrentThreadId());
	OutputDebugStringW(temp);

	return S_OK;
}


客户程序如下:

  1. <p>// TestCom.cpp : Defines the entry point for the console application. 
  2. //</p><p>#include "stdafx.h"</p><p>#include <atlbase.h> 
  3. #include <thread> 
  4. #include <vector> 
  5. #include <windows.h></p><p>#include "../MyCom/MyCom_i.h" 
  6. #include "../MyCom/MyCom_i.c"</p><p>void Test(CComPtr<IMyLine>& spLine) 
  7. WCHAR temp[100] = { 0 }; 
  8. swprintf_s(temp, L"calling thread: %d\n", ::GetCurrentThreadId()); 
  9. OutputDebugStringW(temp);</p><p> HRESULT hr = S_OK; 
  10. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);</p><p> spLine->Draw(CComBSTR(L"yellow"));</p><p> CoUninitialize(); 
  11. }</p><p>int _tmain(int argc, _TCHAR* argv[]) 
  12. CoInitializeEx(NULL, COINIT_MULTITHREADED); 
  13. WCHAR temp[100] = { 0 }; 
  14. swprintf_s(temp, L"Main thread: %d\n", ::GetCurrentThreadId()); 
  15. OutputDebugStringW(temp);</p><p> { 
  16.   CComPtr<IMyLine> spLine; 
  17.   spLine.CoCreateInstance(CLSID_MyLine, NULL, CLSCTX_INPROC);</p><p>  spLine->Draw(CComBSTR(L"red"));</p><p>  std::vector<std::thread> vThreads; 
  18.   for (int i = 0; i < 5; i++) 
  19.   { 
  20.    vThreads.push_back(std::thread(Test, spLine));  // pass a stream instead of com object 
  21.   }</p><p>  for (auto& t: vThreads) 
  22.   { 
  23.    t.join(); 
  24.   } 
  25. }</p><p> 
  26. CoUninitialize();</p><p> return 0; 
  27. }</p><p> </p> 
<p>// TestCom.cpp : Defines the entry point for the console application.
//</p><p>#include "stdafx.h"</p><p>#include <atlbase.h>
#include <thread>
#include <vector>
#include <windows.h></p><p>#include "../MyCom/MyCom_i.h"
#include "../MyCom/MyCom_i.c"</p><p>void Test(CComPtr<IMyLine>& spLine)
{
 WCHAR temp[100] = { 0 };
 swprintf_s(temp, L"calling thread: %d\n", ::GetCurrentThreadId());
 OutputDebugStringW(temp);</p><p> HRESULT hr = S_OK;
 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);</p><p> spLine->Draw(CComBSTR(L"yellow"));</p><p> CoUninitialize();
}</p><p>int _tmain(int argc, _TCHAR* argv[])
{
 CoInitializeEx(NULL, COINIT_MULTITHREADED);
 WCHAR temp[100] = { 0 };
 swprintf_s(temp, L"Main thread: %d\n", ::GetCurrentThreadId());
 OutputDebugStringW(temp);</p><p> {
  CComPtr<IMyLine> spLine;
  spLine.CoCreateInstance(CLSID_MyLine, NULL, CLSCTX_INPROC);</p><p>  spLine->Draw(CComBSTR(L"red"));</p><p>  std::vector<std::thread> vThreads;
  for (int i = 0; i < 5; i++)
  {
   vThreads.push_back(std::thread(Test, spLine));  // pass a stream instead of com object
  }</p><p>  for (auto& t: vThreads)
  {
   t.join();
  }
 }</p><p>
 CoUninitialize();</p><p> return 0;
}</p><p> </p>

上面这段代码很简单,主线程里面创建了一个MTA套间,然后创建一个MTA对象,直接传递个辅助线程。(没有marshal)

之后辅助线程里面,也初始化成MTA模型,直接起了5个线程来调用Draw。结果如下:

可以很清楚的看到,主线程里面的Draw先完成了。(当然,因为那个时候辅助线程还没起)。

之后的5个线程里面,Draw函数就打乱了。线程2616的Draw函数在sleep的时候,其他线程的Draw函数也在运行。

这5个线程用的是同一个com对象。这个跟STA组件完全不同,STA里面都是串行化的,但是MTA这边就不是串行的了。所以MTA组件需要自己实现同步,不然就乱了。

上面的代码例子是:

1. 主线程创建MTA套间;

2. 主线程创建MTA对象;

3. 辅助线程也初始化成MTA;

那么如果辅助线程初始化成STA或者不初始化又如何?

待续。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值