SDC C-Project CF Review 프로그램
LYW
2021-08-09 b354c153b0074e5d54371bc05b12edbe8e613a19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "StdAfx.h"
#include "ThreadPools.h"
 
 
CThreadPools::CThreadPools(int nThreadCount) : m_nThreadCount(nThreadCount)
{
    m_nRollback            = 0;
    m_pPool                = NULL;
    m_pCleanupGroup        = NULL;
 
    CreateThreadPools(m_nThreadCount);
}
 
CThreadPools::~CThreadPools(void)
{
    CloseThreadPools();
}
 
int    CThreadPools::GetThreadCount() const
{
    return m_nThreadCount;
}
 
void CThreadPools::CreateThreadPools(int nThreadCount)
{
    BOOL bRet = FALSE;
 
    TP_CALLBACK_ENVIRON* pCallBackEnviron = (TP_CALLBACK_ENVIRON*)&m_CallBackEnviron;
 
    InitializeThreadpoolEnvironment(pCallBackEnviron);
 
    // Create a custom, dedicated thread pool
    m_pPool = CreateThreadpool(NULL);
 
    if (NULL==m_pPool) 
    {
        CloseThreadPools();
    }
 
    m_nRollback = 1; // pool creation succeeded
 
    // The thread pool is made persistent simply by setting
    // both the minimum and maximum threads to 1.
    SetThreadpoolThreadMaximum(m_pPool, nThreadCount);
 
    bRet = SetThreadpoolThreadMinimum(m_pPool, 1);
 
    if (FALSE==bRet) 
    {
        CloseThreadPools();
    }
 
    //Create a cleanup group for this thread pool
    m_pCleanupGroup = CreateThreadpoolCleanupGroup();
 
    if (NULL==m_pCleanupGroup) 
    {
        CloseThreadPools(); 
    }
 
    m_nRollback = 2;  // Cleanup group creation succeeded
 
    // Associate the callback environment with our thread pool.
    SetThreadpoolCallbackPool(pCallBackEnviron, m_pPool);
 
    // Associate the cleanup group with our thread pool.
    // Objects created with the same callback environment
    // as the cleanup group become members of the cleanup group.
    SetThreadpoolCallbackCleanupGroup(pCallBackEnviron, m_pCleanupGroup, NULL);
 
    m_nRollback = 3;  // Creation of work succeeded
 
}
 
void CThreadPools::CloseThreadPools()
{
    switch (m_nRollback) 
    {
    case 4:
    case 3:        
        // Clean up the cleanup group members.
        CloseThreadpoolCleanupGroupMembers(m_pCleanupGroup, FALSE, NULL);
 
    case 2:
        // Clean up the cleanup group.
        CloseThreadpoolCleanupGroup(m_pCleanupGroup);
        m_pCleanupGroup = NULL;
 
    case 1:
        // Clean up the pool.
        CloseThreadpool(m_pPool);
        m_pPool = NULL;
 
    default:
        break;
    }
}