线程池

实现了一个最简单的线程池,没有考虑惊群现象

#include "pthread.h"
#include "semaphore.h"
#include <list>
#include <vector>
#include <iostream>
using std::list;
using std::vector;
struct thread_job
{
    void(*func)(void *);
    void *arg;
};
class thread_pool
{
public:
    thread_pool(int size_ = 10);
    ~thread_pool();
    void add_job(void (*func)(void*), void *arg);
    static void * route_task(void *);
//private:
    void destroy();
private:
    int   max_threads;        // 线程池中最大线程数限制  
    int   curr_threads;       // 当前线程池中总的线程数  
    int   idle_threads;       // 当前线程池中空闲的线程数 
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    sem_t sem;
    list<thread_job*> job_list;
    vector<pthread_t> thread_handle;
    bool stop_flag;
};
thread_pool::thread_pool(int size_)
{
    stop_flag = false;
    max_threads = size_;
    thread_handle.resize(max_threads);
    curr_threads = 0;
    idle_threads = size_;
    if (pthread_cond_init(&(cond), NULL)) {
        perror("pthread_mutex_init: ");
    }
    if (pthread_mutex_init(&(mutex), NULL)) {
        perror("pthread_mutex_init: ");
    }
    for (int i = 0; i < max_threads; i++){
        if (pthread_create(&(thread_handle[i]), NULL, route_task, this)) {
            perror("pthread_create:");
        }
        //pthread_detach((thread_handle[i]));
    }
}
void * thread_pool::route_task(void *arg)
{
    thread_pool * pool = static_cast<thread_pool *>(arg);
    pthread_mutex_lock(&(pool->mutex));
    while (1)
    {
        if (pool->job_list.empty())
        {
            pool->idle_threads++;
            if (pool->stop_flag){
                pthread_mutex_unlock(&(pool->mutex));
                break;
            }
            {
                pthread_cond_wait(&pool->cond, &(pool->mutex));
            }
            if (pool->stop_flag){
                pthread_mutex_unlock(&(pool->mutex));
                break;
            }
        }
        else
        {
            pool->idle_threads--;
            thread_job* job = pool->job_list.front();
            pool->job_list.pop_front();
            pthread_mutex_unlock(&pool->mutex);
            job->func(job->arg);
            pool->idle_threads++;
            delete job;
            pthread_mutex_lock(&(pool->mutex));
        }
    }
    return NULL;
}
void thread_pool::add_job(void (*func)(void*), void *arg)
{
    thread_job* njob= new thread_job;
    njob->arg = arg;
    njob->func = func;
    pthread_mutex_lock(&mutex);
    if (job_list.empty())
    {
        job_list.push_back(njob);
        pthread_cond_signal(&cond);
    }
    else
    {
        job_list.push_back(njob);
    }
    pthread_mutex_unlock(&mutex);
}
void thread_pool::destroy()
{
    stop_flag = true;
    pthread_cond_broadcast(&cond);
    for each (pthread_t var in thread_handle)
    {
        pthread_join(var, NULL);
    }
}
thread_pool::~thread_pool()
{
    destroy();
}
int ans = 0;
void add1(void *arg)
{
    ans++;
    std::cout << ans << std::endl;
}
int main()
{
    thread_pool pool(40);
    for (int i = 0; i < 1000; i++)
    {
        pool.add_job(add1, NULL);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值