MTA, STA show no change in concurency
This is something I had not expected, hope I am doing something really
wrong. I have 4 ATL out-proc test-sample servers (STA, MTA, BOTH,
FREE) with 3 methods each. I wanted two of the methods to sleep, so I
am using WaitForSingleObject(event). Method1(), Method2() and Method()
blocks for 72sec, 60sec and 0sec.
I have C++ client. All that primary thread does is create 3 threads.
Inside each thread, I call CoInitialize() and create an instance of
COM object. One thread calls only of these Method1() or Method2() or
Method3().
Method3() is supposed to comeback first(has 0sec wait), but it waits
for Method2() to exit i,e 60sec. Then, Method1() counter starts, waits
for another 72 secs. So it means calls getting serialized.
I read DonBox again, as I understand this should not even happen to
STA. Every thread has its own object and own private STA.
I am passing correct param to CoInitializeEx(). I doubt CreateEvent().
Please help me resolve this.
ATL SERVER look like this.
----------------------------------------------------------
CStaTest::Method1(LONG code) // 72sec wait
{
HANDLE hEvent = CreateEvent(0,true,false,0);
CAtlString sLog;
sLog.Format(L"inside Mehod1 %x\r\n",GetCurrentThreadId());
if(hEvent)
{ WaitForSingleObject(hEvent,1000*60*1.2);
CloseHandle(hEvent);
}
return S_OK;
}
CStaTest::Method2(LONG code) // 60sec wait
{
HANDLE hEvent = CreateEvent(0,true,false,0);
CAtlString sLog;
sLog.Format(L"inside Mehod2 %x\r\n",GetCurrentThreadId());
if(hEvent)
{
WaitForSingleObject(hEvent,1000*60*1);
CloseHandle(hEvent);
}
return S_OK;
}
CStaTest::Method3(LONG code) // 0sec wait
{
CAtlString sLog;
sLog.Format(L"inside Mehod3 %x\r\n",GetCurrentThreadId());
return S_OK;
}
CLIENT is as follows, (ex client from FTM)
---------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
CallAptFree();
return 0;
}
void CallAptFree()
{
HRESULT hr = 0;
HANDLE dwTd[4]={0,0,0,0};
unsigned int id = 0;
dwTd[0] = (HANDLE)_beginthreadex( 0, 0, &Func1F, 0, 0, &id );
dwTd[1] = (HANDLE)_beginthreadex( 0, 0, &Func2F, 0, 0, &id );
dwTd[2] = (HANDLE)_beginthreadex( 0, 0, &Func3F, 0, 0, &id );
WaitForMultipleObjects(3,(HANDLE*)dwTd, 1, INFINITE) ;
}
unsigned int __stdcall Func1F(void* pData)
{
HRESULT hr = 0;
CoInitializeEx(0, COINIT_MULTITHREADED);
hr = CoCreateInstance(__uuidof(AptFree), 0, CLSCTX_LOCAL_SERVER ,
__uuidof(IAptFree),(void**)&pIAptSvr1);
if((hr==0) && (pIAptSvr1))
{
hr = pIAptSvr1->raw_Method1(1); //wait
}
CoUninitialize();
return 0;
}
unsigned int __stdcall Func3F(void* pData)
{
HRESULT hr = 0;
CoInitializeEx(0, COINIT_MULTITHREADED);
CAtlStringA sLog;
IAptFree* pIAptSvr1 = 0;
hr = CoCreateInstance(__uuidof(AptFree), 0, CLSCTX_LOCAL_SERVER ,
__uuidof(IAptFree),(void**)&pIAptSvr1);
if((hr==0) && (pIAptSvr1))
{
sLog.Format("\n objA1::Method3 % x\n",GetCurrentThreadId());
printf(sLog.GetBuffer());
hr = pIAptSvr1->raw_Method3(1); //wait
}
CoUninitialize();
return 0;
}
unsigned int __stdcall Func2F(void* pData)
{
HRESULT hr = 0;
CoInitializeEx(0, COINIT_MULTITHREADED);
IAptFree* pIAptSvr1 = 0;
hr = CoCreateInstance(__uuidof(AptFree), 0, CLSCTX_LOCAL_SERVER ,
__uuidof(IAptFree),(void**)&pIAptSvr1);
hr = pIAptSvr1->raw_Method2(0); //wait
CoUninitialize();
return 0;
}
My eventual requirement is, only one instance per thread, ecah
instance should execute simultaneouly. And I am not passing objects
across threads, so I expected STA to suite my needs, but seems even
FTM is also acting same. I am using VS2008. I took out all
OutputDebugString()calls.
Please let me know what could me problem and what model should meet my
needs.
Thanks
Ramesh