| 1 |
/*******************************************************/
|
| 2 |
/* */
|
| 3 |
/* LeafOK Innd */
|
| 4 |
/* Copyright (C) LeafOK.com, 2003-2004 */
|
| 5 |
/* */
|
| 6 |
/* Programmed by Leaf */
|
| 7 |
/* E-mail:leaf@leafok.com QQ:6049044 */
|
| 8 |
/* */
|
| 9 |
/* http://bbs.leafok.com */
|
| 10 |
/* http://bbs.leafok.net */
|
| 11 |
/* http://bbs.fenglin.info */
|
| 12 |
/* */
|
| 13 |
/*******************************************************/
|
| 14 |
|
| 15 |
#pragma once
|
| 16 |
|
| 17 |
#include <time.h>
|
| 18 |
|
| 19 |
#define TS_MAX_THREAD 256
|
| 20 |
#define TS_DEFAULT_TIMEOUT 10000
|
| 21 |
#define TS_SETLOCK_TIMEOUT 100
|
| 22 |
#define TS_SETLOCK_TIMEOUT_MAX 10000
|
| 23 |
|
| 24 |
class thread_pool
|
| 25 |
{
|
| 26 |
public:
|
| 27 |
enum thread_status{
|
| 28 |
S_UNKNOWN = -1,
|
| 29 |
S_UNUSED = 0,
|
| 30 |
S_WORKING = 1,
|
| 31 |
S_WAITING = 2
|
| 32 |
};
|
| 33 |
enum error_code{
|
| 34 |
E_NOERROR = 0,
|
| 35 |
E_LOCK_TIMEOUT = 1,
|
| 36 |
E_MAX_THREAD_EXCEEDED = 2,
|
| 37 |
E_THREAD_NOT_FOUND = 3,
|
| 38 |
E_INVALID_TIMEOUT = 4,
|
| 39 |
E_KILL_THREAD_FAILED = 5
|
| 40 |
};
|
| 41 |
private:
|
| 42 |
UINT uThreadCount;
|
| 43 |
UINT uThreadMax;
|
| 44 |
HANDLE hThreadList[TS_MAX_THREAD];
|
| 45 |
thread_status uThreadStatusList[TS_MAX_THREAD];
|
| 46 |
clock_t time_status_set[TS_MAX_THREAD];
|
| 47 |
clock_t thread_timeout;
|
| 48 |
bool bProcessLock;
|
| 49 |
int SetLock(bool bLock, clock_t tTimeout = TS_SETLOCK_TIMEOUT);
|
| 50 |
error_code uLastErrorCode;
|
| 51 |
public:
|
| 52 |
thread_pool(UINT uThreadMax = TS_MAX_THREAD, clock_t thread_timeout = TS_DEFAULT_TIMEOUT);
|
| 53 |
~thread_pool(void);
|
| 54 |
int GetLastError(void);
|
| 55 |
int AddThread(HANDLE hThread);
|
| 56 |
int RemoveThread(HANDLE hThread);
|
| 57 |
int SetThreadStatus(HANDLE hThread, int uStatus);
|
| 58 |
int GetThreadStatus(HANDLE hThread);
|
| 59 |
int KillDeadThread(void);
|
| 60 |
};
|