gpt4 book ai didi

c++ - 线程池正在占用所有 CPU 资源

转载 作者:行者123 更新时间:2023-11-28 00:54:35 26 4
gpt4 key购买 nike

我最近一直在研究我自己的线程池实现,它运行良好(非常好)。问题是它占用了几乎所有的 CPU 资源。请看一下我的实现:

线程池.h

#include <vector>
#include <list>
#include <Windows.h>

const int DefaultThreadsNumber = 5;

class thread_pool
{
typedef void ( *Task )( );

void* m_hEvent;

CRITICAL_SECTION m_hExecuteFunc;
CRITICAL_SECTION m_hRunFunc;


std::vector<void*> m_ThreadHandles;
std::list<Task> m_TasksQueue;
int m_nThreadInQueue;
static unsigned __stdcall Run( void* thisParam );
public:
thread_pool( int nMaxThreadNo );
~thread_pool();
void Execute( Task task );
};

线程池.cpp

#include "thread_pool.h"
#include <process.h>

void* g_hTaskFinishedEvent;

thread_pool::thread_pool( int nMaxThreadNo )
{
if ( nMaxThreadNo <= 0 )
nMaxThreadNo = DefaultThreadsNumber;

m_nThreadInQueue = nMaxThreadNo;
for( int i = 0; i < nMaxThreadNo; ++i )
{
m_ThreadHandles.push_back((void*)_beginthreadex(NULL, 0, &Run, (void*)this, 0, 0 ));
}

InitializeCriticalSection( &m_hExecuteFunc );
InitializeCriticalSection( &m_hRunFunc );
}


thread_pool::~thread_pool()
{
for ( std::vector<void*>::iterator it = m_ThreadHandles.begin();
it != m_ThreadHandles.end();
++it )
{
CloseHandle( *it );
}

DeleteCriticalSection( &m_hExecuteFunc );
DeleteCriticalSection( &m_hRunFunc );

}

void thread_pool::Execute( Task task )
{
EnterCriticalSection( &m_hExecuteFunc );

m_TasksQueue.push_back( task );

LeaveCriticalSection( &m_hExecuteFunc );

m_hEvent = CreateEvent(NULL, true, false, NULL);
SetEvent( m_hEvent ); // TODO: what if setEvent will fail???
}

unsigned __stdcall thread_pool::Run(void* thisParam )
{
thread_pool *This = (thread_pool*)thisParam;
while(true)
{
WaitForSingleObject( This->m_hEvent, INFINITE );
while(!This->m_TasksQueue.empty())
{


EnterCriticalSection( &This->m_hExecuteFunc );

if ( !This->m_TasksQueue.empty() )
{
This->m_TasksQueue.front()();
This->m_TasksQueue.pop_front();

g_hTaskFinishedEvent = CreateEvent(NULL, true, false, NULL);
SetEvent( g_hTaskFinishedEvent );
}

LeaveCriticalSection( &This->m_hExecuteFunc );
}
}
return 0;
}

如何改进?

提前致谢。

最佳答案

在 CreateEvent 中,bManualReset 设置为 true,但您并未在任何地方调用 ResetEvent。因此,一旦发出信号,事件将永远保持设置状态,并且 WaitForSingleObject 会立即返回。使其成为自动重置事件,或在所有工作人员完成后调用 ResetEvent

关于c++ - 线程池正在占用所有 CPU 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12215516/

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