线程池实践 - wridtedb.cpp

WriteDBUnit::~WriteDBUnit()
{
    if (writedb_thread_count_ > 0)
    {
        threadpool_destroy(&threadpool_);
    }
}

void WriteDBUnit::init(const char* configfilepath)
{

writedb_thread_count_ = get_quote_server().config().quote_param_.writedb_thread_count;
    if (writedb_thread_count_ > 0)
    {
        threadpool_init(&threadpool_, writedb_thread_count_);
    }
}

void WriteDBUnit::OnReceive(int sessionid, int transactionid, const std::string& msg, long long timestamp)
{

//取消线程池
            if (writedb_thread_count_ > 0)
            {
                threadpool_add_task(&threadpool_, map_handle_message_[transactionid], msg);
            }
            else
            {
                map_handle_message_[transactionid](msg);
            }

}


bool WriteDBUnit::threadpool::pop_task(std::list<WriteDBUnit::task_t>& ls_task)
{
    ls_task.clear();

    condition_lock(&cd_task);

    while (!que_task.empty())
    {
        task_t& pRet = que_task.front();
        ls_task.push_back(pRet);
        que_task.pop();

        if ((int)ls_task.size() >= pop_task_count)
            break;
    }

    condition_unlock(&cd_task);

    return ls_task.size() > 0;
}

bool WriteDBUnit::threadpool::push_thread(pthread_t pid)
{
    bool bRet = false;

    condition_lock(&cd_thread);

    if ((int)ls_pthreadid.size() < max_threads)
    {
        ls_pthreadid.insert(pid);
        bRet = true;
    }

    condition_unlock(&cd_thread);

    return bRet;
}

void WriteDBUnit::threadpool::pop_thread(pthread_t pid)
{
    condition_lock(&cd_thread);

    if (ls_pthreadid.find(pid) != ls_pthreadid.end())
    {
        ls_pthreadid.erase(pid);
    }

    condition_unlock(&cd_thread);
}

//创建的线程执行
void *thread_routine(void *arg)
{
    WriteDBUnit::threadpool_t *pool = (WriteDBUnit::threadpool_t *)arg;

    if (!pool->push_thread(pthread_self()))
        return NULL;

    STLOG_INFO << "WriteDBUnit::thread is starting : " << pthread_self();

    //// 超时时间:秒
    //int timeout = 30;
    //if (pool->writedb_thread_sleep * 10 > timeout)
    //{
    //    timeout = pool->writedb_thread_sleep * 10;
    //}

    //struct timespec abstime_last;
    //clock_gettime(CLOCK_REALTIME, &abstime_last);

    while (!pool->quit)
    {
        std::list<WriteDBUnit::task_t> ls_task;
        bool bRet = pool->pop_task(ls_task);

        // 没有消息则休眠
        if (!bRet)
        {
            sleep(pool->writedb_thread_sleep);

            //struct timespec abstime_new;
            //clock_gettime(CLOCK_REALTIME, &abstime_new);

            //if (abstime_new.tv_sec - abstime_last.tv_sec >= timeout)
            //{
            //    STLOG_INFO << "WriteDBUnit::thread wait timed out : " << pthread_self();
            //    pool->pop_thread(pthread_self());
            //    break;
            //}

            continue;
        }

        for (auto it = ls_task.begin(); it != ls_task.end(); ++it)
        {
            WriteDBUnit::task_t task = *it;
            //执行任务
            task.run(task.arg);
            //执行完任务释放内存
            //delete task;
        }

        //clock_gettime(CLOCK_REALTIME, &abstime_last);
    }

    STLOG_INFO << "WriteDBUnit::thread is exiting : " << pthread_self();
    return NULL;
}

//增加一个任务到线程池
void WriteDBUnit::threadpool_add_task(threadpool_t *pool, HandleMessage run, const std::string& arg)
{
    //产生一个新的任务
    task_t newtask;
    newtask.run = run;
    newtask.arg = arg;
    //strncpy(newtask->arg, arg.c_str(), sizeof(newtask->arg)-1);

    pool->push_task(newtask);

    ////当前线程池中线程个数没有达到设定的最大值,创建一个新的线程
    //if ((int)pool->ls_pthreadid.size() < pool->max_threads)
    //{
    //    pthread_t tid;
    //    pthread_create(&tid, NULL, thread_routine, pool);
    //}
}

//线程池初始化
void WriteDBUnit::threadpool_init(threadpool_t *pool, int threads)
{
    STLOG_INFO << "WriteDBUnit::threadpool_init threadcount = " << threads;

    pool->max_threads = threads;
    pool->quit = false;

    for (int i = 0; i < threads; ++i)
    {
        pthread_t tid;
        pthread_create(&tid, NULL, thread_routine, pool);
    }
}

//线程池销毁
void WriteDBUnit::threadpool_destroy(threadpool_t *pool)
{
    STLOG_INFO << "WriteDBUnit::threadpool_destroy";

    //如果已经调用销毁,直接返回
    if (pool->quit)
    {
        return;
    }
    //设置销毁标记为1
    pool->quit = true;

    // 等待线程退出
    for (auto it = pool->ls_pthreadid.begin(); it != pool->ls_pthreadid.end(); ++it)
    {
        pthread_join(*it, nullptr);
    }
    STLOG_INFO << "WriteDBUnit::threadpool_destroy pthread_join finish";

    // 清除未完成的任务
    while (!pool->que_task.empty())
    {
        //task_t ptask = pool->que_task.front();

        //if (ptask != nullptr)
        //{
        //    //ptask->run(ptask->arg);
        //    free(ptask);
        //}
        
        pool->que_task.pop();
    }
    STLOG_INFO << "WriteDBUnit::threadpool_destroy cleartask finish";
}

猜你喜欢

转载自blog.csdn.net/gaoli8393/article/details/88306544