Synchronizes access to the target method.




The synchronize C++ attribute implements support for synchronizing the target method of an object. Synchronization allows multiple objects to use a common resource (such as a method of a class) by controlling the access of the target method.

The code inserted by this attribute calls the proper Lock method (determined by the threading model) at the beginning of the target method. When the method is exited, Unlock is automatically called. For more information on these functions, see CComAutoThreadModule::Lock

This attribute requires that the coclass, progid, or vi_progid attribute (or another attribute that implies one of these) also be applied to the same element. If any single attribute is used, the other two are automatically applied. For example, if progid is applied, vi_progid and coclass are also applied.


The following code provides synchronization for the UpdateBalance method of the CMyClass object.

// cpp_attr_ref_synchronize.cpp
// compile with: /LD
#include "atlbase.h"
#include "atlcom.h"


class CMyClass {
   float m_nBalance;

   void UpdateBalance(float nAdjust) {
      m_nBalance += nAdjust;


Attribute Context

Applies to Class method, method
Repeatable No
Required attributes One or more of the following: coclass, progid, or vi_progid.
Invalid attributes None

For more information about the attribute contexts, see Attribute Contexts.

See also

COM Attributes