-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.h
executable file
·57 lines (49 loc) · 1.46 KB
/
ThreadPool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// @Author Lin Ya
// @Email [email protected]
// This file has not been used
#pragma once
#include "Channel.h"
#include <pthread.h>
#include <functional>
#include <memory>
#include <vector>
const int THREADPOOL_INVALID = -1;
const int THREADPOOL_LOCK_FAILURE = -2;
const int THREADPOOL_QUEUE_FULL = -3;
const int THREADPOOL_SHUTDOWN = -4;
const int THREADPOOL_THREAD_FAILURE = -5;
const int THREADPOOL_GRACEFUL = 1;
const int MAX_THREADS = 1024;
const int MAX_QUEUE = 65535;
typedef enum
{
immediate_shutdown = 1,
graceful_shutdown = 2
} ShutDownOption;
struct ThreadPoolTask
{
std::function<void(std::shared_ptr<void>)> fun;
std::shared_ptr<void> args;
};
class ThreadPool
{
private:
static pthread_mutex_t lock;
static pthread_cond_t notify;
static std::vector<pthread_t> threads;
static std::vector<ThreadPoolTask> queue;
static int thread_count;
static int queue_size;
static int head;
// tail 指向尾节点的下一节点
static int tail;
static int count;
static int shutdown;
static int started;
public:
static int threadpool_create(int _thread_count, int _queue_size);
static int threadpool_add(std::shared_ptr<void> args, std::function<void(std::shared_ptr<void>)> fun);
static int threadpool_destroy(ShutDownOption shutdown_option = graceful_shutdown);
static int threadpool_free();
static void *threadpool_thread(void *args);
};