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

C++ 线程池正在占用所有CPU资源,c++,winapi,threadpool,C++,Winapi,Threadpool,我最近一直在研究我自己的线程池实现,它工作得很好(相当不错)。问题是它占用了几乎所有的CPU资源。 请看一下我的实现: ThreadPool.h #include <vector> #include <list> #include <Windows.h> const int DefaultThreadsNumber = 5; class thread_pool { typedef void ( *Task )( ); void* m_hE

我最近一直在研究我自己的线程池实现,它工作得很好(相当不错)。问题是它占用了几乎所有的CPU资源。 请看一下我的实现:

ThreadPool.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 );
};
#包括
#包括
#包括
const int DefaultThreadsNumber=5;
类线程池
{
typedef void(*任务)();
void*m_hEvent;
临界截面m_hExecuteFunc;
关键部门人力资源基金会;
std::向量m_螺纹柄;
std::列出m_TasksQueue;
int m_n读取队列;
静态未签名的stdcall运行(void*thisParam);
公众:
线程池(int nMaxThreadNo);
~thread_pool();
无效执行(任务);
};
ThreadPool.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;
}
#包括“thread_pool.h”
#包括
void*g_hTaskFinishedEvent;
线程池::线程池(int nMaxThreadNo)
{
if(nMaxThreadNo m_hEvent,无限);
而(!This->m_TasksQueue.empty())
{
EnterCriticalSection(&This->m_hExecuteFunc);
如果(!This->m_TasksQueue.empty())
{
这->m_TasksQueue.front();
这->m_TasksQueue.pop_front();
g_hTaskFinishedEvent=CreateEvent(NULL、true、false、NULL);
设置事件(g_hTaskFinishedEvent);
}
离开CriticalSection(&This->m_hExecuteFunc);
}
}
返回0;
}
如何改进


提前感谢。

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

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

逐步查看消耗100%CPU的代码。你应该很快发现问题。什么时候需要所有的CPU?当没有线程运行时?这可能是因为您等待
m_hEvent
,但在调用
Execute
之前不初始化它。我不知道当使用非法事件句柄调用时,
WaitForSingleObject
会做什么。您的代码还有其他设计问题,比如将
传递给您创建的所有线程,这意味着所有线程都将使用相同的
thisParam
。还要确保提供线程函数的退出路径,CloseHandle不会终止treads.Joachim Pileborg-传递它只是为了在单独的线程中运行类方法。另外,程序中只使用了一个threadPool实例,所以我想将这个类设置为singleton,否则这不是一个好主意?请仔细阅读消耗100%CPU的代码。你应该很快发现问题。什么时候需要所有的CPU?当没有线程运行时?这可能是因为您等待
m_hEvent
,但在调用
Execute
之前不初始化它。我不知道当使用非法事件句柄调用时,
WaitForSingleObject
会做什么。您的代码还有其他设计问题,比如将
传递给您创建的所有线程,这意味着所有线程都将使用相同的
thisParam
。还要确保提供线程函数的退出路径,CloseHandle不会终止treads.Joachim Pileborg-传递它只是为了在单独的线程中运行类方法。另外,程序中只使用了一个threadPool实例,所以我想将这个类设置为singleton,否则这不是一个好主意?而且,
m\u hEvent
不断被一个新事件覆盖。另外,
m\u hEvent
不断被一个新事件覆盖。