COM线程模型 - MTA接口 - Part IV - (运行线程)
2014-11-07 11:29
405 查看
From : http://blog.csdn.net/zj510/article/details/39053647
一个STA对象只能属于一个STA套间,那么一个STA对象一定是在一个线程里面运行的。所以STA对象不需要考虑并发,因为它永远是串行运行的。
那么一个MTA对象在哪个线程里面运行的?
MTA套间调用MTA对象
看下面这段代码:
[cpp]
view plaincopyprint?
// 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对象是运行在系统创建的线程里面,看前面一篇文章。
一个STA对象只能属于一个STA套间,那么一个STA对象一定是在一个线程里面运行的。所以STA对象不需要考虑并发,因为它永远是串行运行的。
那么一个MTA对象在哪个线程里面运行的?
MTA套间调用MTA对象
看下面这段代码:
[cpp]
view plaincopyprint?
// 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;
}
// 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对象是运行在系统创建的线程里面,看前面一篇文章。
相关文章推荐
- COM线程模型 - MTA接口 (运行线程)
- COM线程模型 - STA接口 - Part III (MTA客户,跨线程传递COM对象)
- COM线程模型 - MTA接口 (STA套间调用MTA对象)
- COM线程模型 - MTA接口 - Part II - (传递MTA COM对象给STA套间线程)
- COM线程模型 - STA接口 (MTA客户,跨线程传递COM对象)
- COM线程模型 - MTA接口
- COM线程模型 - MTA接口 - Part I
- COM线程模型 - MTA接口 - Part III -(STA套间调用MTA对象)
- COM线程模型 - MTA接口 (传递MTA COM对象给STA套间线程)
- COM线程模型 - STA接口 - Part II -(跨线程传递对象,消息循环)
- COM线程模型 - STA接口 (跨线程传递对象,消息循环)
- COM线程模型 - STA接口
- 泛说"COM线程模型"
- CLR 无法从 COM 上下文 0x1a2740 转换为 COM 上下文 0x1a28b0,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- COM线程模型详解
- COM线程模型
- COM线程模型-套间
- COM线程模型-套间(转)
- 理解套间(涉及进程、线程、COM线程模型)(转载)