[c++]线程池简单例子(with barrier)

简单实现了一版线程池,为了弄epoll匆匆写的,,,,,如果有什么问题的话也麻烦指出来,,,多谢?

 

通过addTask提交任务。任务需要继承Worker并实现void run()。

// MyThreadPool/MyThreadPool.h                                                                                                                                                                    
#ifndef __MY_THREAD_POOL_H__
#define __MY_THREAD_POOL_H__
#include 
#include 
#include 
#include 
#include 
using namespace std;

class Worker;
class MyThread;
class MyThreadPool;

class Worker
{
    public:
        Worker(){};
        virtual ~Worker(){printf("[ERROR] error ~Worker()\n");};
        virtual void run(){printf("[ERROR] error run()\n");};
};

class MyThread
{
    public:
        MyThreadPool* pool;
        pthread_t* m_thread;

        MyThread():pool(){};
        ~MyThread(){};
        bool init();
        bool join();
        static void* run(void* ptr);
};

class MyThreadPool
{
    public:
        vector m_threads;
        vector task_vec;
        bool stopFlag;

        pthread_attr_t m_attr;
        pthread_mutex_t m_lock;
        pthread_barrier_t m_barr;

        bool open(const int& num);
        bool join();
        bool getTask(Worker*& task);
        bool addTask(Worker* task);

};


#endif
// MyThreadPool/MyThreadPool.cpp                                                                                                                                                                  
#include "MyThreadPool.h"
bool MyThread::init()
{
    m_thread = (pthread_t*)malloc(sizeof(pthread_t)); //开辟内存
    pthread_create(m_thread, &(pool->m_attr), MyThread::run, pool );
    return true;
}
bool MyThread::join()
{
    pthread_join(*m_thread,NULL);
    if(m_thread != NULL) free(m_thread);
    return true;
}
void* MyThread::run(void* ptr)
{
    MyThreadPool* pool = (MyThreadPool*)ptr;
    pthread_barrier_wait(&(pool->m_barr));
//  printf("start run!\n");
    Worker* task;
    while(1)
    {   
        if( pool->stopFlag && pool->task_vec.empty() )
            break;
//      printf("try:get\n");
        if(pool->getTask(task))
        {   
//          printf("get this task :%x\n", task);
            task->run();
        }   
        else usleep(100000);
    }   
    return NULL;
}

bool MyThreadPool::open(const int& num)
{
    pthread_attr_init(&m_attr);
    pthread_mutex_init(&m_lock, NULL);
    pthread_barrier_init(&m_barr, NULL, num+1); //初始化栅栏
    pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_JOINABLE);//设置线程属性为joinable线程
    pthread_attr_setstacksize(&m_attr, 1024000); //设置线程堆栈大小,b
    stopFlag=false;
    m_threads.reserve(num);
    for(int i = 0; i < num; ++i)
    {
        MyThread* p = new MyThread();
        p->pool = this;
        p->init();
        m_threads.push_back(p);
//      printf("open one thread : %x\n", p);
    }
    pthread_barrier_wait(&m_barr);
    pthread_attr_destroy(&m_attr);
    return true;
}
bool MyThreadPool::join()
{
    stopFlag=true;
    for(int i = 0; i < m_threads.size(); ++i)
    {
        m_threads[i]->join();
//      printf("join one thread : %x\n", m_threads[i]);
        delete m_threads[i];
        m_threads[i] = NULL;
    }
    pthread_mutex_destroy(&m_lock);
    pthread_barrier_destroy(&m_barr);
    return true;
}
bool MyThreadPool::getTask(Worker*& task)
{
//  printf("getTask\n");
    pthread_mutex_lock(&m_lock);
    if(!task_vec.empty())
    {
        task = *task_vec.begin();
        task_vec.erase(task_vec.begin());
    }
    else
        task = NULL;
    pthread_mutex_unlock(&m_lock);
//  printf("get %x, size:%d\n",task, task_vec.size());
    return task==NULL ? false : true;
}
bool MyThreadPool::addTask(Worker* task)
{
    pthread_mutex_lock(&m_lock);
    task_vec.push_back(task);
    pthread_mutex_unlock(&m_lock);
//  printf("add %x, size:%d\n",task, task_vec.size());
    return true;
}
// main/main.cpp                                                                                                                                                                                  
#include 
#include 
#include "MyThreadPool/MyThreadPool.h"
using namespace std;
class A : public Worker
{
    public:
        int a,b;
        int ret;
        A(){};
        virtual ~A(){};
        virtual void run(){
            //printf("A.run() start, %d %d\n", a,b);
            printf("curr_task : %x | %d %d\n", this,a,b);
            ret = a + b;
            sleep(1);
            //printf("A.run() finish, %d\n", ret);
        };  
};

int main(int argc,char **argv)
{
    MyThreadPool pool;
    pool.open(3);
    for(int i = 0 ; i < 8 ;i ++) 
    {   
        A *a = new A();
        a->a = i;
        a->b = 10; 
        pool.addTask(a);
        //printf("add task!, %d\n", i);
    }   
    pool.join();
    return 0;
}

后续再实现个 1.链表替换vector  2.提供查询任务是否结束的接口和直接结束某任务的接口。

哎,,又挖了坑,,,,

你可能感兴趣的:(c++)