手撕线程池c++

#include "threadpool.h"

pthread_mutex_t ThreadPool::mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ThreadPool::cond = PTHREAD_COND_INITIALIZER;
std::vector<Task*> ThreadPool::tasks;

ThreadPool::ThreadPool(int threads) 
{  
    this->threads = threads;  
    this->shutdown = false;  
    this->counter = 0;  
    this->idle = 0;
}

void* ThreadPool::process(void *arg) 
{  
    struct timespec abstime;  
    int timeout;  
    ThreadPool *pool = static_cast<ThreadPool*>(arg);  
    printf("thread 0x%0x is starting\n", (int)pthread_self());  
    while (true) 
    {  
        pthread_mutex_lock(&mutex);  
        pool->idle++;  
        timeout = 0;     
        while (tasks.empty() && !pool->shutdown) 
        {  
            clock_gettime(CLOCK_REALTIME, &abstime);  
            abstime.tv_sec += 2;  
            int status = pthread_cond_timedwait(&cond, &mutex, &abstime);  
            if (status == ETIMEDOUT) 
            {  
                printf("thread 0x%0x is waiting time out\n", 
(int)pthread_self());                              
                timeout = -1;  break;  
            }  
        }  
        pool->idle--;  
        if (!tasks.empty()) 
        {  
            Task *task = tasks.back();  
            tasks.pop_back();  // 执行任务  
            pthread_mutex_unlock(&mutex);  
            task->run(task->arg);  
            delete task;  
            pthread_mutex_lock(&mutex);  
        }  
        if (pool->shutdown && tasks.empty()) 
        {  
            pool->counter--;  
            if (pool->counter == 0) 
            {  
                pthread_cond_signal(&cond);  
            }  
            pthread_mutex_unlock(&mutex);  
            break;  
        }  
        if (timeout == -1 && tasks.empty()) 
        {  
            pool->counter--;  
            pthread_mutex_unlock(&mutex);  
            break;  
        }  
        pthread_mutex_unlock(&mutex);  }  
        printf("thread 0x%0x is exiting\n", (int)pthread_self());  
return NULL;
}

void ThreadPool::add_task(void* (*run)(void *arg), void *arg) 
{  
    pthread_mutex_lock(&mutex);  // 生成新的任务  
    Task *task = new Task;  
    task->run = run;  
    task->arg = arg;  
    tasks.push_back(task);  // 如果有等待线程,则唤醒一个  
    if (idle > 0) 
    {  
        pthread_cond_signal(&cond);  
    } 
    else if (counter < threads)
    {  
        pthread_t tid;  
        pthread_create(&tid, NULL, ThreadPool::process, (void*)this);  
        this->counter++;  
    }  
    pthread_mutex_unlock(&mutex);
}

void ThreadPool::threadpool_destroy() 
{  
    if (shutdown) {  return;  }  
    pthread_mutex_lock(&mutex);  
    shutdown = true;  
    if (counter > 0) 
    {  // 唤醒空闲线程退出  
        if (idle > 0) 
        {  
            pthread_cond_broadcast(&cond);  
        }  // 如果线程处于执行阶段,等待任务执行完毕之后退出  
        while (counter > 0) { pthread_cond_wait(&cond, &mutex);  }  }              
        pthread_mutex_unlock(&mutex);  
        pthread_mutex_destroy(&mutex);
}

还有一个头文件

#ifndef _THREADPOOL_H
#define _THREADPOOL_H

#include <vector>
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <iostream>

struct Task {  
    void* (*run)(void *arg);  void *arg;
};

class ThreadPool {
    private:  int threads;  
    static std::vector<Task*> tasks;  
    static pthread_mutex_t mutex;  
    static pthread_cond_t cond;  
    static void *process(void *arg);  
    bool shutdown;  
    int counter;  
    int idle;
    public:  ThreadPool(int threads_);  ~ThreadPool() {};  
    void add_task(void* (*task)(void *arg), void *arg);  
    void threadpool_destroy();
};

#endif /*_THREADPOOL_H*/

https://www.nowcoder.com/discuss/111851?source_id=profile_create&channel=666

 

还有一个c++11的参考写法:

https://blog.csdn.net/qq_41681241/article/details/86723964

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值