gpt4 book ai didi

c++ - 如何在线程完成工作时触发事件

转载 作者:行者123 更新时间:2023-11-28 04:49:16 27 4
gpt4 key购买 nike

<分区>

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

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

例子:

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

帮我解决这个问题。

提前致谢。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com