如何在线程完成其工作时触发事件

时间:2018-01-31 06:06:28

标签: c++ multithreading events threadpool

用例:在C ++中创建线程池,并将工作分配给所有线程。我想在来自threadpool的线程完成其工作时触发事件。

但我不知道当线程池中的线程完成其工作时如何触发事件。

示例:

void ThreadPoolImpl::InitThreadPool(){
pool = CreateThreadpool(NULL);
if (pool == NULL)
{
    //Log("Could not create a thread pool!");
    return;
}
InitializeThreadpoolEnvironment(&environment);
cleanupGroup = CreateThreadpoolCleanupGroup();
if (cleanupGroup == NULL)
{
    //Log("Could not create a thread pool cleanup group!");
}
SetThreadpoolCallbackPool(&environment, pool);
SetThreadpoolCallbackCleanupGroup(&environment, cleanupGroup, NULL);}


void ThreadPoolImpl::RunThreads(int const iThreadCount)

{

dwThreadCount = iThreadCount;
if (dwThreadCount == 0)
{
    dwThreadCount = DEFAULT_THREAD_COUNT;
}
SetThreadpoolThreadMaximum(pool, dwThreadCount);
SetThreadpoolThreadMinimum(pool, MIN_THREAD_COUNT);


work = CreateThreadpoolWork(workcallback,NULL,&environment);

if (NULL == work) {
    _tprintf(_T("CreateThreadpoolWork failed. LastError: %u\n"),
        GetLastError());
}

SubmitThreadpoolWork(work);//Want to trigger an event for each thread in thread pool

}

帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

为每个帖子使用condition_variable