头文件
#include <thread>
#include <mutex>
#include <vector>
#include <queue>
#include <condition_variable>
namespace base {
class ThreadPool {
public:
static const int MAX_THREAD_NUM = 3;
enum PRIORITY {
LEVEL_1 = 1,
LEVEL_2,
LEVEL_3,
};
typedef std::function<void()> Task;
typedef std::pair<PRIORITY, Task> TaskPair;
typedef std::vector<std::thread*> Threads;
struct TaskPriorityCmp {
bool operator () (const ThreadPool::TaskPair p1, const ThreadPool::TaskPair p2) {
return p1.first > p2.first;
}
};
typedef std::priority_queue<TaskPair, std::vector<TaskPair>, TaskPriorityCmp> Tasks;
ThreadPool();
~ThreadPool();
bool start();
bool stop();
bool add_task(const Task& task);
bool add_task(const TaskPair& task);
void thread_loop();
ThreadPool::Task