Ignore:
Timestamp:
Oct 2, 2013, 8:20:03 AM (10 years ago)
Author:
roman
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Utilities/Miscellaneous/StaticConstructor/StaticConstructor.cpp

    r219 r220  
    66#include <atlbase.h>
    77#include <atlcom.h>
     8
     9class CModule :
     10        public CAtlExeModuleT<CModule>
     11{
     12};
    813
    914class CFoo
     
    2025                m_nValue++;
    2126        }
     27        CFoo(INT nValue)
     28        {
     29                m_nValue = nValue;
     30                Sleep(100);
     31                m_nValue++;
     32        }
     33        CFoo(INT nValue1, INT nValue2)
     34        {
     35                m_nValue = nValue1 + nValue2;
     36                Sleep(100);
     37                m_nValue++;
     38        }
    2239};
    2340
    24 CRITICAL_SECTION g_Section;
     41#define STATICLOCAL_PART1(type, name) \
     42        static BYTE g_pn##name##Data[sizeof (type)]; \
     43        static BOOL g_b##name##Initialized = FALSE; \
     44        type& g_##name = reinterpret_cast<type&>(g_pn##name##Data); \
     45        { \
     46                ATLASSERT(_pAtlModule); \
     47                CComCritSecLock<CComCriticalSection> Lock(_pAtlModule->m_csStaticDataInitAndTypeInfo); \
     48                if(!g_b##name##Initialized) \
     49                { \
     50                        new (g_pn##name##Data) type(
     51
     52#define STATICLOCAL_PART2(type, name) \
     53                        ); \
     54                        g_b##name##Initialized = TRUE; \
     55                } \
     56        }
     57
     58#define STATICLOCAL0(type, name) \
     59        STATICLOCAL_PART1(type, name) STATICLOCAL_PART2(type, name)
     60#define STATICLOCAL1(type, name, parameter1) \
     61        STATICLOCAL_PART1(type, name) parameter1 STATICLOCAL_PART2(type, name)
     62#define STATICLOCAL2(type, name, parameter1, parameter2) \
     63        STATICLOCAL_PART1(type, name) parameter1, parameter2 STATICLOCAL_PART2(type, name)
    2564
    2665DWORD WINAPI ThreadProc(INT_PTR*)
    2766{
    2867        #if TRUE
    29                 static BYTE g_pnFooData[sizeof (CFoo)];
    30                 static BOOL g_bFooInitialized = FALSE;
    31                 CFoo& g_Foo = reinterpret_cast<CFoo&>(g_pnFooData);
    32                 EnterCriticalSection(&g_Section);
    33                 if(!g_bFooInitialized)
    34                 {
    35                         new (g_pnFooData) CFoo();
    36                         g_bFooInitialized = TRUE;
    37                 }
    38                 LeaveCriticalSection(&g_Section);
     68                //STATICLOCAL0(CFoo, Foo);
     69                //STATICLOCAL1(CFoo, Foo, 10);
     70                STATICLOCAL2(CFoo, Foo, 20, 30);
    3971        #else
    4072                static CFoo g_Foo;
     
    4678int _tmain(int argc, _TCHAR* argv[])
    4779{
    48         InitializeCriticalSection(&g_Section);
     80        CModule Module;
    4981        HANDLE phObjects[32];
    5082        for(SIZE_T nIndex = 0; nIndex < _countof(phObjects); nIndex++)
Note: See TracChangeset for help on using the changeset viewer.