0

我正在尝试处理类中的线程池。下面是我的代码。

#include <Windows.h>

class ClassA
{
public : // user API
    ClassA()
    {
    }

    ~ClassA()
    {
    }

public : //my muiti-thread func

    void init()
    {
        //*************************************
        // multithread Initialization
        //*************************************
        pool = NULL;
        cleanupgroup = NULL;
        rollback = 0;
        bool bRet = false;

        pool = CreateThreadpool(NULL);
        if(NULL == pool)
        {
            goto cleanPool;
        }
        rollback = 1;                                       

        SetThreadpoolThreadMaximum(pool, 5);                
        bRet = SetThreadpoolThreadMinimum(pool, 10);

        if (FALSE == bRet) {
            goto cleanPool;
        }

        cleanupgroup = CreateThreadpoolCleanupGroup();

        if (NULL == cleanupgroup) {
            goto cleanPool; 
        }

        rollback = 2;                                       

        SetThreadpoolCallbackPool(&CallBackEnviron, pool);

        SetThreadpoolCallbackCleanupGroup(&CallBackEnviron,
            cleanupgroup,
            NULL);

        return ;

cleanPool:
        switch (rollback) 
        {
        case 2:
            // Clean up the cleanup group.
            CloseThreadpoolCleanupGroup(cleanupgroup);

        case 1:
            // Clean up the pool.
            CloseThreadpool(pool);

        default:
            break;
        }

        return ;
    }

    void foo()
    {
        PTP_WORK work = NULL;
        work = CreateThreadpoolWork(ClassA::_delegate,
                        NULL, 
                        &CallBackEnviron);
    }

    static void __stdcall _delegate(PTP_CALLBACK_INSTANCE Instance, PVOID Parameter, PTP_WORK Work)
    {
        //some code
    }

    PTP_POOL pool;
    UINT rollback;
    TP_CALLBACK_ENVIRON CallBackEnviron;    
    PTP_CLEANUP_GROUP cleanupgroup;
};

int main()
{
    ClassA a;
    a.init();
    a.foo();
}

如果您创建一个项目并执行此代码,它会得到未处理的执行...我不知道为什么...

4

1 回答 1

1

我认为异常是由未初始化的结构 CallBackEnviron 引起的。文档说明这个结构必须由InitializeThreadpoolEnvironment 初始化

于 2013-07-08T08:37:38.137 回答