c++实现的线程池代码

#ifndef _THREADPOOL_H
#define _THREADPOOL_H
#include <vector>
#include <queue>
#include <thread>
#include <iostream>
#include <condition_variable>
using namespace std;
template <typename T>
class threadPool
{
public:
    threadPool(int threadNumber = 1);
    ~threadPool();
    bool append(T* task);
    static void* worker(void* arg);
public:
    void threadPoolRun();
    void threadPoolStop();
private:
    queue<T*> taskQueue;
    vector<thread> workThread;
    std::mutex threadPoolMtx;
    std::condition_variable threadPoolCv;
    const int MAX_THREADS = 100;
    bool stop;
};
template <typename T>
threadPool<T>::threadPool(int threadNumber) : stop(false)
{
    if (threadNumber <= 0 || threadNumber > MAX_THREADS) {
        throw exception();
    }
    for (int i = 0; i < threadNumber; i++) {
        workThread.emplace_back(worker, this);
    }
}
template <typename T>
inline threadPool<T>::~threadPool() {
    {
        unique_lock<std::mutex> unique(threadPoolMtx);
        stop = true;
    }
    threadPoolCv.notify_all();
    for (auto& wt : workThread) {
        wt.join();
    }
}
template <typename T>
bool threadPool<T>::append(T* task) {
    unique_lock<mutex> unique(threadPoolMtx);
    taskQueue.push(task);
    unique.unlock();
    threadPoolCv.notify_one();
    return true;
}
template <typename T>
void* threadPool<T>::worker(void* arg) {
    threadPool* pool = (threadPool*)arg;
    pool->threadPoolRun();
    return pool;
}
template <typename T>
void threadPool<T>::threadPoolRun() {
    while (!stop) {
        unique_lock<mutex> unique(this->threadPoolMtx);
        while (this->taskQueue.empty()) {
            this->threadPoolCv.wait(unique);
        }
        if (!(this->taskQueue.empty())) {
            T* task = this->taskQueue.front();
            this->taskQueue.pop();
            if (task) {
                task->process();
            }
        }
    }
}
template <typename T>
void threadPool<T>::threadPoolStop() {
    ~threadPool();
}
#endif

main.cpp

#include<iostream>
#include"ThreadPool.h"
#include <string>
using namespace std;
class Task
{
public:
    void process()
    {
        cout << "task" << endl;
        std::cout << "id = " << this_thread::get_id() << std::endl;
        //this_thread::sleep_for(chrono::seconds(1));
    }
};
int main(void)
{
   threadPool<Task> pool(100);
   while (1) {
        Task* task1 = new Task();
        Task* task5 = new Task();
        Task* task6 = new Task();
        Task* task7 = new Task();
        Task* task8 = new Task();
        Task* task9 = new Task();
        Task* task10 = new Task();
        Task* task11 = new Task();
        Task* task12 = new Task();
        pool.append(task1);
        pool.append(task5);
        pool.append(task6);
        pool.append(task7);
        pool.append(task8);
        pool.append(task9);
        pool.append(task10);
        pool.append(task11);
        pool.append(task12);
        cout << "task successful2! " << endl;
        delete task1;
        delete task5;
        delete task6;
        delete task7;
        delete task8;
        delete task9;
        delete task10;
        delete task11;
        delete task12;
        cout << "task successful3! " << endl;
   }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的C语言实现线程池代码: ``` #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAX_THREADS 10 #define MAX_QUEUE 65536 typedef struct { void *(*func)(void *); void *arg; } threadpool_task_t; typedef struct { pthread_mutex_t lock; pthread_cond_t notify; pthread_t *threads; threadpool_task_t *queue; int thread_count; int queue_size; int head; int tail; int count; int shutdown; int started; } threadpool_t; threadpool_t *threadpool_create(int thread_count, int queue_size, int flags) { threadpool_t *pool; int i; (void) flags; if (thread_count <= 0 || thread_count > MAX_THREADS || queue_size <= 0 || queue_size > MAX_QUEUE) { return NULL; } if ((pool = (threadpool_t *)malloc(sizeof(threadpool_t))) == NULL) { return NULL; } pool->thread_count = 0; pool->queue_size = queue_size; pool->head = pool->tail = pool->count = 0; pool->shutdown = pool->started = 0; pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); pool->queue = (threadpool_task_t *)malloc(sizeof(threadpool_task_t) * queue_size); if ((pthread_mutex_init(&(pool->lock), NULL) != 0) || (pthread_cond_init(&(pool->notify), NULL) != 0) || (pool->threads == NULL) || (pool->queue == NULL)) { goto error; } for (i = 0; i < thread_count; i++) { if (pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void*)pool) != 0) { threadpool_destroy(pool, 0); return NULL; } pool->thread_count++; pool->started++; } return pool; error: if (pool) { threadpool_free(pool); } return NULL; } int threadpool_add(threadpool_t *pool, void *(*func)(void *), void *arg) { int err = 0; int next; if (pool == NULL || func == NULL) { return -1; } if (pthread_mutex_lock(&(pool->lock)) != 0) { return -1; } next = pool->tail + 1;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值