-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadpool.h
More file actions
78 lines (68 loc) · 2.16 KB
/
Copy paththreadpool.h
File metadata and controls
78 lines (68 loc) · 2.16 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Created by Jason.Z on 2020/6/23.
//
#ifndef SOCKETSERVERCPLUS_THREADPOOL_H
#define SOCKETSERVERCPLUS_THREADPOOL_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;
};
/**
* @struct threadpool
* @brief The threadpool struct
*
* @var notify Condition variable to notify worker threads.
* @var threads Array containing worker threads ID.
* @var thread_count Number of threads
* @var queue Array containing the task queue.
* @var queue_size Size of the task queue.
* @var head Index of the first element.
* @var tail Index of the next element.
* @var count Number of pending tasks
* @var shutdown Flag indicating if the pool is shutting down
* @var started Number of started threads
*/
void myHandler(std::shared_ptr<void> req);
class ThreadPool
{
public:
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;
static int running_threads;
static int waiting_threads;
public:
static int threadpool_create(int _thread_count, int _queue_size);
static int threadpool_add(const std::shared_ptr<void>& args, const std::function<void(std::shared_ptr<void>)>& fun = myHandler);
static int threadpool_destroy(ShutDownOption shutdown_option = graceful_shutdown);
static int threadpool_free();
static void *threadpool_thread(void *args);
};
#endif //SOCKETSERVERCPLUS_THREADPOOL_H