一个自创的多线程池设计-升级版

本文改进了之前的线程池设计,通过引入任务池的概念,将任务抽象为对象,提高了任务管理和模块内聚度。文章详细展示了如何设计任务基类及派生任务类,并结合线程池进行调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上篇博文中,我们自己设计实现了一个简单的多线程池,功能实现很简单,里面有很多的不足,例如对于线程的回调函数,只是在以普通的函数来实现,在此我们想借以任务的形式来给线程池线程指定相应的任务,因而来实现一个任务池+线程池的设计理念,下面我们就开始设计我们的任务,代码如下:

#ifndef __TASK__H
#define __TASK__H

#include <stdio.h>
#include <stdlib.h>

class Task
{
    public:
        Task(){}
        virtual ~Task(){}
    public:
        virtual void doWork()
        {
            printf("This is a Task\n");
        }
};


class PrintTask : public Task
{
    public:
        PrintTask(){}
        ~PrintTask(){}
    public:
        void doWork()
        {
            printf("This is a PrintTask\n");
        }
};

class CacluateTask : public Task
{
    public:
        CacluateTask(){}
        ~CacluateTask(){}
    public:
        void doWork()
        {
            printf("This is a CacluateTask\n");
        }
};
class CommTask : public Task
{
    public:
        CommTask(){}
        ~CommTask(){}
    public:
        void doWork()
        {
            printf("This is a CommTask\n");
        }
};
#endif

接下来,我们需要稍微修改下我们的线程池代码,代码如下:

#ifndef __THREAD__H
#define __THREAD__H

#include <pthread.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include "Task.h"

enum threadState
{
    THREAD_NORMAL,
    THREAD_BUSY,
    THREAD_KILL
};
class Thread
{
    public:
        Thread(Task* fun,const std::string threadName = std::string()):threadName(threadName)
        {
            this->fun = fun;
            threadHandle = -1;
            state = THREAD_NORMAL;
        }
        ~Thread(){}
        void setThreadFun(Task* fun)
        {
            this->fun = fun;
        }
        void run();
        void stop();
    private:
        static void* start(void* arg)
        {
            Thread* thd = static_cast<Thread*>(arg);
            printf("%s:",thd->threadName.c_str());
            thd->fun->doWork();
            return NULL;

        }
    public:
        pthread_t threadHandle;
        Task* fun;
        std::string threadName;
        threadState state;
};
class ThreadPool
{
    public:
        ThreadPool(const int threadNum):threadNum(threadNum)
        {
            threadPool.clear();
        }
        ~ThreadPool()
        {
        }
        void addThread(const int threadNum,Task* f);
        void delThread(const int threadNum);
        void schedule();
        void startAll();
        void stopAll();
        Thread* assignThread();
    private:
        int threadNum;
        std::vector<Thread*> threadPool;
};
#endif

再来看看我们的测试用例程序吧,代码如下:

#include "Thread.h"
#include "Task.h"

int main()
{
    PrintTask printTask;
    CacluateTask cacluateTask;
    CommTask commTask;
    int threadNum = 10;
    ThreadPool threadPool(10);
    threadPool.addThread(threadNum,&printTask);

    threadPool.startAll();
    threadPool.stopAll();
    sleep(1);

    Thread* thread = threadPool.assignThread();
    assert(thread);

    thread->setThreadFun(&cacluateTask);
    thread->run();
    sleep(1);

    thread = threadPool.assignThread();
    assert(thread);
    thread->setThreadFun(&commTask);
    thread->run();

    sleep(5);
    threadPool.delThread(threadNum);
    return 0;
}

总结

     这篇博文主要是针对上篇博文的改进,主要的改进:将线程的回调函数全部抽象为对象,这样设计的好处就是易于管理不同的任务,以及将抽象与实现进行分离,从而提高模块的内聚度,在接下来的博文中,我们将会设计实现一个多线程多任务池的东西,将上面的东西进行整合,加深对线程池和任务池的理解,多谢了,

如果需要,请注明转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值