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

Last change on this file since 219 was 219, checked in by roman, 10 years ago
File size: 1.2 KB
RevLine 
[219]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 CFoo
10{
11public:
12        INT m_nValue;
13
14public:
15// CFoo
16        CFoo()
17        {
18                m_nValue++;
19                Sleep(100);
20                m_nValue++;
21        }
22};
23
24CRITICAL_SECTION g_Section;
25
26DWORD WINAPI ThreadProc(INT_PTR*)
27{
28        #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);
39        #else
40                static CFoo g_Foo;
41        #endif
42        _tprintf(_T("Thread %05d, Value %02d\n"), GetCurrentThreadId(), g_Foo.m_nValue);
43        return 0;
44}
45
46int _tmain(int argc, _TCHAR* argv[])
47{
48        InitializeCriticalSection(&g_Section);
49        HANDLE phObjects[32];
50        for(SIZE_T nIndex = 0; nIndex < _countof(phObjects); nIndex++)
51                phObjects[nIndex] = AtlCreateThread<INT_PTR>(&ThreadProc, 0);
52        WaitForMultipleObjects(_countof(phObjects), phObjects, TRUE, INFINITE);
53        return 0;
54}
55
Note: See TracBrowser for help on using the repository browser.