线程池+日志

lockGuard.hpp

cpp 复制代码
#include <iostream>
#include <pthread.h>


class Mutex
{
public:
    Mutex(pthread_mutex_t *pmtx)
        : _pmtx(pmtx)
    {
    }
    void lock()
    {
        //std::cout << "要进行加锁:" << std::endl;
        pthread_mutex_lock(_pmtx);
    }
    void unlock()
    {
        //std::cout << "要进行解锁:" << std::endl;
        pthread_mutex_unlock(_pmtx);
    }

private:
    pthread_mutex_t *_pmtx;
};

//RAII风格的加锁方式
class lockGuard
{
public:
    lockGuard(pthread_mutex_t *pmtx)
        : _mtx(pmtx)
    {
        _mtx.lock();
    }
    ~lockGuard()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};

log.hpp

cpp 复制代码
#pragma once
#include <iostream>  
#include <string>  
#include <cstdarg>  
#include <cstdio>  
#include <ctime>

#define DEBUG 0
#define NORMAL 1  
#define WARNING 2  
#define ERROR 3  
#define FATAL 4  

const char* gLevelMap[5] = { "DEBUG","NORMAL","WARNING","ERROR","FATAL"};

#define LOGFILE "./threadpool.log"

void logMessage(int level, const char* format, ...)
{
    char stdBuffer[1024];//标准部分
    time_t timestamp = time(nullptr);
    struct tm* time=localtime(&timestamp);
    snprintf(stdBuffer,sizeof stdBuffer, "[%s][%ld]",gLevelMap[level],timestamp);



    char logBuffer[64]; // 自定义部分
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof(logBuffer), format, args); // 使用 vsnprintf 而不是 vsprintf  
    va_end(args);

    FILE* fp=fopen(LOGFILE,"a");

    //打印到显示器
    //printf("%s %s\n",stdBuffer,logBuffer);

    //打印到指定文件
    fprintf(fp,"%s%s\n",stdBuffer,logBuffer);

    fclose(fp);
}

Makefile

cpp 复制代码
cp: testMain.cc
	g++ -o cp testMain.cc -std=c++11 -lpthread  
  
.PHONY: clean  
  
clean:  
	rm -rf cp

Task.hpp

cpp 复制代码
#include <iostream>
#include <string>
#include "log.hpp"

// typedef std::function<int(int, int)> func_t;

typedef int (*Func_t)(int, int);

int add(int x, int y)
{
    return x + y;
}

class Task
{
public:
    Task() {}

    Task(int x, int y, Func_t func = add)
        : _x(x), _y(y), _func(func)
    {
    }

    void operator()(const std::string& name)
    {
        logMessage(WARNING,"%s处理完成: %d+%d=%d | %s |%d",name.c_str(),_x,_y,_func(_x,_y),__FILE__,__LINE__);
    }

private:
    int _x;
    int _y;
    Func_t _func;
};

testMain.cc

cpp 复制代码
#include "threadPool.hpp"
#include "Task.hpp"
#include "log.hpp"


int main()
{
    //logMessage(0,"%s %d %c","我爱你",520,'u');
    srand((unsigned int)time(nullptr));
    ThreadPool<Task> *tp = new ThreadPool<Task>(10);
    tp->run();

    while (true)
    {
        int x = rand() % 100 + 1;
        usleep(7721);
        int y = rand() % 100 + 1;
        Task t(x,y,[](int x,int y)->int{return x+y;});

        logMessage(DEBUG,"制作任务完成: %d+%d=?",x,y);

        tp->pushTask(t);
    }
}

thread.hpp

cpp 复制代码
#include <iostream>
#include <pthread.h>
#include <string>
// #include <functional>

// typedef std::function<void* (void*)> func_t;

typedef void *(*func_t)(void *);

class ThreadData
{
public:
    std::string _name;
    void *_args;
};

class Thread
{
public:
    Thread(int num, func_t callback, void *args)
        : _func(callback)
    {
        std::string str = "Thread " + std::to_string(num);
        _tdata._name = str;
        _tdata._args = args;
    }

    void start()
    {
        pthread_create(&_tid, nullptr, _func, &_tdata);
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }

    std::string name()
    {
        return _tdata._name;
    }

    ~Thread()
    {
        pthread_join(_tid, nullptr);
    }

public:
    // std::string _name;
    // void *_args;
    ThreadData _tdata;
    pthread_t _tid;
    func_t _func;
};

threadPool.hpp

cpp 复制代码
#include <iostream>
#include <pthread.h>
#include <vector>
#include <queue>
#include <unistd.h>

#include "thread.hpp"
#include "lockGuard.hpp"
#include "log.hpp"

const int default_capacity = 5;

template <class T>
class ThreadPool
{
public:
    ThreadPool(int capacity = default_capacity)
        : _capacity(capacity)
    {
        pthread_mutex_init(&_mtx, nullptr);
        pthread_cond_init(&_cond, nullptr);
        for (int i = 0; i < capacity; i++)
        {
            Thread *ptd = new Thread(i + 1, routine, this);
            _threads.push_back(ptd);
        }
    }

    bool isEmpty()
    {
        return _task_queue.empty();
    }

    static void *routine(void *arg)
    {
        ThreadData *td = (ThreadData *)arg;
        ThreadPool<T> *tp = (ThreadPool<T> *)td->_args;
        while (true)
        {
            lockGuard lockguard(&tp->_mtx);
            if (tp->isEmpty())
            {
                pthread_cond_wait(&tp->_cond, &tp->_mtx);
            }
            T task = tp->_task_queue.front();
            tp->_task_queue.pop();
            task(td->_name);
            sleep(1);
        }
    }

    void run()
    {
        for (auto &iter : _threads)
        {
            iter->start();
            //std::cout << iter->name() << " 启动成功" << std::endl;
            logMessage(NORMAL,"%s%s",iter->name().c_str(),"启动成功");
        }
    }

    void pushTask(const T &task)
    {
        lockGuard lockguard(&_mtx);
        _task_queue.push(task);
        pthread_cond_signal(&_cond);
    }

    void joins()
    {
        for (int i = 0; i < _capacity; i++)
        {
            _threads[i]->join();
        }
    }

    ~ThreadPool()
    {
        for (int i = 0; i < _capacity; i++)
        {
            _threads[i]->join();
            delete _threads[i];
        }
        pthread_mutex_destroy(&_mtx);
        pthread_cond_destroy(&_cond);
    }

private:
    std::vector<Thread *> _threads; // 存放线程
    size_t _capacity;

    std::queue<T> _task_queue; // 存放任务

    pthread_mutex_t _mtx;
    pthread_cond_t _cond;
};
相关推荐
wkd_00718 分钟前
【开源库 | xlsxio】C/C++读写.xlsx文件,xlsxio 在 Linux(Ubuntu18.04)的编译、交叉编译
c语言·c++·xlsxio·c语言读写xlsx·c++读写xlsx·xlsxio交叉编译
IT 古月方源31 分钟前
ensp 关于ARRP 的讲解 配置
运维·开发语言·网络·tcp/ip·智能路由器
西猫雷婶36 分钟前
python学opencv|读取图像(二十二)使用cv2.polylines()绘制多边形
开发语言·python·opencv
温轻舟39 分钟前
前端开发 -- 自定义鼠标指针样式
开发语言·前端·javascript·css·html·温轻舟
xiaocaibao7771 小时前
编程语言的软件工程
开发语言·后端·golang
Ocean☾1 小时前
C语言-基因序列转换独热码(one-hot code)
c语言·开发语言
2401_858286111 小时前
117.【C语言】数据结构之排序(选择排序)
c语言·开发语言·数据结构·笔记·算法·排序算法
捕鲸叉1 小时前
C++软件设计模式之类型模式和对象型模式
开发语言·c++·设计模式
魔道不误砍柴功1 小时前
Java 中反射的高级用法:窥探 Java 世界的魔法之门
java·开发语言·python
2401_857617621 小时前
“无缝购物体验”:跨平台网上购物商城的设计与实现
java·开发语言·前端·安全·架构·php