#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <pthread.h>
#include <unistd.h>
using namespace std;
int g_task_id = 1;
struct TaskInfo;
typedef int (*TaskFunc)(TaskInfo* task);
std::mutex con_var_mu_;
std::condition_variable cond_var_;
std::mutex task_queue_mu_;
struct TaskInfo
{
int task_id;
TaskFunc callback;
int param1;
int param2;
};
void* thread_func(void* param);
class ThreadPool
{
public:
ThreadPool(int num);
~ThreadPool();
private:
ThreadPool();
ThreadPool(const ThreadPool& rhs);
ThreadPool& operator=(const ThreadPool& rhs);
public:
void Init();
void AddTask(TaskInfo* task);
public:
static ThreadPool& Instance()
{
static ThreadPool pool = ThreadPool(2);
return pool;
}
public:
bool stoped_;
std::vector<pthread_t*> threadids_;
std::queue<TaskInfo*> tasks_;
};
void ThreadPool::Init()
{
cout << "pool init..." << endl;
}
ThreadPool::ThreadPool(int num) : stoped_(false)
{
for (int i = 0; i < num; i++) {
pthread_t *cur_t = new pthread_t;
pthread_create(cur_t, NULL, thread_func, this);
threadids_.push_back(cur_t);
cout << "init a thread:" << *cur_t << endl;
}
cout << "*****************************************************************" << endl;
}
ThreadPool::~ThreadPool()
{
stoped_ = true;
con_var_mu_.lock();
cond_var_.notify_all();
con_var_mu_.unlock();
for (int i = 0; i < threadids_.size(); i++) {
pthread_join(*threadids_[i], NULL);
}
for (int i = 0; i < threadids_.size(); i++) {
delete threadids_[i];
}
task_queue_mu_.lock();
while (!tasks_.empty()) {
TaskInfo* task = tasks_.front();
delete task;
tasks_.pop();
}
task_queue_mu_.unlock();
}
void ThreadPool::AddTask(TaskInfo* task)
{
task_queue_mu_.lock();
tasks_.push(task);
task_queue_mu_.unlock();
std::unique_lock<std::mutex> lk(con_var_mu_);
cond_var_.notify_one();
}
int task_func(TaskInfo* task)
{
cout << "task id:" << task->task_id << " execute..." << endl;
cout << "param1: " << task->param1 << " plus param2: " << task->param2 << ", result: " << task->param1 + task->param2 << endl;
}
void* thread_func(void* param)
{
while (true) {
std::unique_lock<std::mutex> lk(con_var_mu_);
while (ThreadPool::Instance().tasks_.size() == 0 && !ThreadPool::Instance().stoped_) {
cond_var_.wait(lk);
}
if (ThreadPool::Instance().stoped_) {
cout << "thread:" << pthread_self() << " stoped." << endl;
pthread_exit(NULL);
}
cout << "thread:" << pthread_self() << " get a task..." << endl;
task_queue_mu_.lock();
TaskInfo* task = ThreadPool::Instance().tasks_.front();
ThreadPool::Instance().tasks_.pop();
task_queue_mu_.unlock();
task->callback(task);
delete task;
}
return NULL;
}
int main()
{
ThreadPool::Instance().Init();
for (int i = 0; i < 10; i++) {
TaskInfo* task = new TaskInfo();
task->callback = task_func;
task->param1 = i;
task->param2 = i * 5;
task->task_id = g_task_id++;
ThreadPool::Instance().AddTask(task);
}
sleep(2);
}