Reentrancy problem in STA

Reentrancy problem in STA

Post by r0d0lf » Wed, 17 Nov 2004 07:56:26


Hello,

I have a COM component in an STA implemented with ATL Apartment
Threading.
The exe is installed as a Windows service.

My client is also a COM component which is running also as a separate
Windows service but probably in an MTA.

I am experiencing the following problem: The client instantiates my
component on several threads concurently and calls my function. I see
that my function is executed always by the same thread (because of
STA). But before my function is completed, it will be called
(reentered) again. I actually see it on the trace lines.

How can I avoid or handle this kind of situation specially thinking
about the handling of global and static members?

My function is actually calling a function on a third COM component as
the code snippet shows.

Could you please help?

Thank you:

Tibor



STDMETHODIMP CMyComp::MyFunc(
long lUniqueID,
BSTR * pbstrVersion,
VARIANT * pvarInstallDate)
{
USES_CONVERSION;
TRACE("CMyComp::MyFunc()\n");
CString strParams;
strParams.Format("UniqueID = %ld, ThreadID = %ld\n",
lUniqueID,
GetCurrentThreadId());
TRACE(strParams);

if (!m_pWebService)
{
ERROR("WebService not initialized.\n");
return S_FALSE;
}

struct Data lData;

HRESULT hr = S_OK;

try
{
lData = m_pWebService->getData(
lUniqueID
);

TRACE("getData ok: softwareVersion:%s, installationDate:%s\n",
lData.softwareVersion,
lData.installationDate);

*pbstrVersion = ::SysAllocString(A2COLE(lData.softwareVersion));
pvarInstallDate->vt = VT_DATE;
HRESULT hr = ::VarDateFromStr(
A2W(lData.installationDate),
0,
0,
&(pvarInstallDate->date));

if ( FAILED(hr) )
{
ERROR("Unable to convert date:%s\n",
(LPCTSTR)_bstr_t(lData.installationDate));
return hr;
}
}
catch (const _com_error& e)
{
hr = e.Error();
ERROR("getData failed: hr = %X, Desc = %s\n",
hr, (const char *)e.Description());
ERROR(strParams);
}
return hr;
}
 
 
 

Reentrancy problem in STA

Post by Alexander » Wed, 17 Nov 2004 08:40:49

There are two approaches - rewrite your code to be reentrant,
so the reentrancy doesn't matter, or rewrite it to be thread-safe
and switch to MTA service (which makes much more sense for
a service anyway). I'd choose the latter...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: XXXX@XXXXX.COM
MVP VC FAQ: http://www.yqcomputer.com/
=====================================

 
 
 

Reentrancy problem in STA

Post by tibor.fodo » Mon, 06 Dec 2004 18:35:37

ello Alexander,

Thank you for your hint.

I rewrote the component to be thread-safe, used CSemaphore for
synchronization, and it workes fine!

I do not know how would it be possible to rewrite the code to be
reentrant?
Is there any good example out there?

Tibor

"Alexander Nickolov" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...
 
 
 

Reentrancy problem in STA

Post by Alexander » Tue, 07 Dec 2004 15:18:04

nfortunately, reentrancy is not taught as well as multi-
threading and synchronization, so I can't point you to
a book. You need to use boolean flags to protect the
state, always have your internal state consistent and
reexamine it after a method call returns (don't use
stack variables for holding state). Sounds simple,
but it's not. I hate doing reentrant code, multi-threading
is so much more straightforward...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: XXXX@XXXXX.COM
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Tibor Fodor" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...