source: trunk/Utilities/Miscellaneous/StaticConstructor/StaticConstructor.cpp @ 220

Last change on this file since 220 was 220, checked in by roman, 10 years ago
File size: 2.0 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2013
3// Created by Roman Ryltsov roman@alax.info
4
5#include "stdafx.h"
6#include <atlbase.h>
7#include <atlcom.h>
8
9class CModule :
10        public CAtlExeModuleT<CModule>
11{
12};
13
14class CFoo
15{
16public:
17        INT m_nValue;
18
19public:
20// CFoo
21        CFoo()
22        {
23                m_nValue++;
24                Sleep(100);
25                m_nValue++;
26        }
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        }
39};
40
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)
64
65DWORD WINAPI ThreadProc(INT_PTR*)
66{
67        #if TRUE
68                //STATICLOCAL0(CFoo, Foo);
69                //STATICLOCAL1(CFoo, Foo, 10);
70                STATICLOCAL2(CFoo, Foo, 20, 30);
71        #else
72                static CFoo g_Foo;
73        #endif
74        _tprintf(_T("Thread %05d, Value %02d\n"), GetCurrentThreadId(), g_Foo.m_nValue);
75        return 0;
76}
77
78int _tmain(int argc, _TCHAR* argv[])
79{
80        CModule Module;
81        HANDLE phObjects[32];
82        for(SIZE_T nIndex = 0; nIndex < _countof(phObjects); nIndex++)
83                phObjects[nIndex] = AtlCreateThread<INT_PTR>(&ThreadProc, 0);
84        WaitForMultipleObjects(_countof(phObjects), phObjects, TRUE, INFINITE);
85        return 0;
86}
87
Note: See TracBrowser for help on using the repository browser.