急, 一个线程池(自编)与网络SOCKET的程序,STL的代码发生问题,那位大虾帮帮忙
//线程池(自编)
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_
#pragma warning(disable: 4530)
#pragma warning(disable: 4786)
#include <cassert>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
#include <windows.h>
#include <process.h>
class thread_pool
{
protected:
static DWORD WINAPI work_proc(LPVOID lpParameter = NULL);//工作线程
struct work_item; //工作数据结构
struct thread_item; //线程数据结构
queue<work_item *> _work_queue; //工作队列
vector<thread_item *> _threads;//线程数据
CRITICAL_SECTION _cs_work_queue, _cs_threads; //工作队列临界, 线程数据临界
HANDLE _event_end, _event_complete, _semaphore_call;//结束事件, 完成事件, 工作信号
long _thread_num, _runing_num; //线程数, 运行的线程数
void add(int num)//添加线程
{
EnterCriticalSection(&_cs_threads);
for(int _i=0, i; _i<num; _i++)
{
i = _i + _thread_num;
_threads.push_back(new thread_item( this));
_threads[i]->_handle = CreateThread(NULL, 0, work_proc, _threads[i], 0, &(_threads[i]->_thread_id));
assert(_threads[i]->_handle);
}
_thread_num += num;
LeaveCriticalSection(&_cs_threads);
}
public:
struct work_item
{
void (*_fun)(void *);//用户函数
void *_para; //用户参数
work_item(void (*fun)(void *) = NULL, void *para = NULL) : _fun(fun), _para(para)
{ };
};
struct thread_item
{
unsigned long _thread_id; //线程ID
HANDLE _handle; //线程句柄
thread_pool* _this; //线程池的指针
thread_item(thread_pool *pthis) : _this(pthis), _handle(NULL)
{ };
};
thread_pool(long thread_num = 4);
~thread_pool();
void call(void (*fun)(void *), void *para = NULL)
{
assert(fun);
EnterCriticalSection(&_cs_work_queue);
_work_queue.push(new work_item(fun, para));
LeaveCriticalSection(&_cs_work_queue);
ReleaseSemaphore(_semaphore_call, 1, NULL);
}
void wait_end(long time_limit = INFINITE)//结束线程池, 并同步等待
{
SetEvent(_event_end);
WaitForSingleObject(_event_complete, time_limit);
}
void end()//结束线程池
{
SetEvent(_event_end);
}
};
thread_pool::thread_pool(long thread_num) : _thread_num(0), _runing_num(0)
{
InitializeCriticalSection(&_cs_work_queue);
InitializeCriticalSection(&_cs_threads);
_event_complete = CreateEvent(0, false, false, NULL);
_event_end = CreateEvent(0, true, false, NULL);
_semaphore_call = CreateSemaphore(0, 0, 0x7FFFFFFF, NULL);
assert(_semaphore_call && _event_complete && _event_end);
add(thread_num <= 0 ? 4 : thread_num);
}
thread_pool::~thread_pool()
{
DeleteCriticalSection(&_cs_work_queue);
DeleteCriticalSection(&_cs_threads);
CloseHandle(_event_end);
CloseHandle(_event_complete);
CloseHandle(_semaphore_call);
while(!_work_queue.empty())
{
delete _work_queue.front();
_work_queue.pop();
}
for(vector<thread_item*>::iterator iter=_threads.begin(); iter!=_threads.end(); iter++)
{
if(*iter)
{
CloseHandle((*iter)->_handle);
delete *iter;
}
}
}
DWORD WINAPI thread_pool::work_proc(LPVOID lpParameter)
{
thread_item * t_item = (thread_item*)lpParameter;
thread_pool *tp = t_item->_this;
HANDLE handle[2];
handle[0] = tp->_semaphore_call;
handle[1] = tp->_event_end;
InterlockedIncrement(&tp->_runing_num);
work_item * w_item = NULL;
bool do_proc;
for(;;)
{
DWORD wr = WaitForMultipleObjects(2, handle, false, INFINITE);
EnterCriticalSection(&tp->_cs_work_queue);
if(do_proc = !tp->_work_queue.empty())
{
w_item = tp->_work_queue.front();
tp->_work_queue.pop();
}
LeaveCriticalSection(&tp->_cs_work_queue);
if(wr == WAIT_OBJECT_0 + 1 && !do_proc)
break;
if(do_proc && w_item)
{
w_item->_fun(w_item->_para);
delete w_item;
}
}
InterlockedDecrement(&tp->_runing_num);
if(!tp->_runing_num)
SetEvent(tp->_event_complete);
return 0;
};
#endif