AddRef and Release function of COM inteface

G

Guest

Hello everyone,


I have noticed a lot of COM samples that the function AddRef and Release
function of COM inteface is not synchronized to make it thread safe when
dealing with adding/decreasing reference count.

Is it needed to synchronize AddRef and Release? Or the COM runtime
underlying implementation handles thread safe issue by itself (i.e.
transparent to upper layer COM server developer)

I am using Visual Studio 2005 to develop native (unmanaged C++) COM. No
other framework is used (e.g. ATL).


thanks in advance,
George
 
C

Crest Teethgel

Is it needed to synchronize AddRef and Release? Or the COM runtime

Yes.
Why and how are described in Don Box's Essential COM (p. 53)
 
G

Guest

Thanks Crest,


I want to confirm with you that you mean we need to add thread safe
synchronization approach to AddRef and Release to make counter value thread
safe manually by developers (i.e. COM runtime engine does not provide such
function)?


regards,
George
 
C

Carl Daniel [VC++ MVP]

George said:
Thanks Crest,


I want to confirm with you that you mean we need to add thread safe
synchronization approach to AddRef and Release to make counter value
thread safe manually by developers (i.e. COM runtime engine does not
provide such function)?

Yes, that's correct. AddRef/Release are typically implemented using
InterlockedIncrement and InterlockedDecrement.

-cd
 
B

Ben Voigt [C++ MVP]

George said:
Thanks Crest,


I want to confirm with you that you mean we need to add thread safe
synchronization approach to AddRef and Release to make counter value
thread
safe manually by developers (i.e. COM runtime engine does not provide such
function)?

*If* you mark your component with the single-threaded or apartment threading
model then you needn't worry.

Other threading models need to use InterlockedXYZ functions as Carl
mentioned.
 
G

Guest

Hi Ben,


I am interested to learn the basics of apartment thread model. Could you
recommend some learning materials?


regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top