项目中的线程池和Log日志实现记录

本文介绍了一个简单的线程池实现方案,包括线程池的结构、任务队列管理、条件变量的使用以及线程间的同步机制。同时,还提供了一个基本的日志模块实现,用于记录不同级别的日志信息。

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

1.项目中线程池的简单实现:

#ifndef __THREAD_POOL_HPP__
#define __THREAD_POLL_HPP__
/*线程池
 *1.线程池中有五个线程
 *2.利用条件变量
 *3.在队列总添加任务
 *  queue<Task> task_queue
 *4.加锁 
 *   pthread_mutex_t lock
 *5.唤醒
 *   pthread_cond_t cond
 *6.任务类
 *
 */
#include<iostream>
#include<pthread.h>
#include<queue>
#include<sys/time.h>
#include<unistd.h>
#define NUM 5
#include"Log.hpp"
using namespace std;

typedef int (*handler_t) (int);

//任务结构
class Task
{
	private:
		int sock_;
		handler_t handler_;
	public:
		Task()
		{
		    sock_ = -1;
		    handler_ = NULL;
		}
                void SetTask(int sock,handler_t handler)
                {
                  sock_ = sock;
                  handler_ = handler;
                }
		void Run()
		{
			handler_(sock_);
		}

		~Task()
		{}
};

ThreadPool


//线程池
class ThreadPool
{
	private:
		int thread_total_num;
		int thread_idle_num;
		queue<Task> task_queue;
		pthread_mutex_t lock;
      		pthread_cond_t cond;
                bool isquit;
        private:
               void LockQueue()
                {
                 pthread_mutex_lock(&lock);
                }

                void UnLockQueue()
                {
                 pthread_mutex_unlock(&lock);
                }
                
                void GetTask(Task & t)
                {
                   t = task_queue.front();
                   task_queue.pop();
                }
                bool IsEmpty()
                {

                   if(task_queue.size() == 0)
                      return true;
                   return false;

                }

               void ThreadWait()
                {    
                     //如果线程池退出了则线程退出
                     if(isquit)
                     {
                     //退出前解锁
                     UnLockQueue();
                     thread_total_num--;
                     pthread_exit((void*)0);
                     }
                     pthread_cond_wait(&cond,&lock);
                     thread_idle_num++;
                }

                void WeakUpThread()
                {
                  pthread_cond_signal(&cond);
                  thread_idle_num--;
                }
                void WeakUpAllThread()
                {
                     
                      pthread_cond_broadcast(&cond);
                }

               //如果不是static会有this指针
               static void* Thread_Routine(void* arg)
                {
                    //分离线程
                    ThreadPool* tp = (ThreadPool*)arg;
                    pthread_detach(pthread_self()); 
                    while(1)
                     {
                       tp-> LockQueue();
                        while(tp->IsEmpty())
                        {
                          //等待
                         tp-> ThreadWait();
                        }
                        //取任务
                        Task t;
                        tp->GetTask(t);
                        //解锁
                       tp-> UnLockQueue();
                        //处理任务
                        t.Run();
                     }
                }
	public:
		ThreadPool(int num_ = NUM):thread_total_num(num_),thread_idle_num(0)
	        {
			pthread_mutex_init(&lock,NULL);
			pthread_cond_init(&cond,NULL);
	        }   

		void InitThreadPool()
		{
			int i = 0;
			for(i;i < NUM;i++)
			{
				pthread_t id[NUM];
				pthread_create(&id[i],NULL,Thread_Routine,this);
			}
                     
                    LOG(INFO,"create thread");
		}

                

                void PushTask(Task& t)
                {
                    //加锁
                    LockQueue();
                    //如果是需要退出了则不能在继续添加线程
                    if(isquit)
                      {
                        UnLockQueue();
                        return;
                      }
                    //添加任务
                    task_queue.push(t);
                    //唤醒线程
                    WeakUpThread();
                    //解锁
                    UnLockQueue();
                }
 
                void StopThread()
                {
                    //加锁
                    LockQueue();
                    //isqueue设置为true
                    isquit = true;
                    //解锁
                    UnLockQueue();
                    if(thread_idle_num > 0)
                      {
                        WeakUpAllThread();
                      }

                }
 
		~ThreadPool()
		{
			pthread_mutex_destroy(&lock);
			pthread_cond_destroy(&cond);
		}
};

#endif

项目中Log的简单实现

#ifndef __LOG_HPP__
#define __LOG_HPP__

#include<iostream>
#include<string>
#include<sys/time.h>
using namespace std;

#define INFO 0
#define DEBUG 1
#define WARNING 2
#define ERROR 3

uint64_t GetTimeStamp()
{
	struct timeval time_;
	gettimeofday(&time_,NULL);
	return time_.tv_sec;
}

string GetLogLevel(int level_){
	switch(level_){

		case 0:
			return "INFO";
		case 1:
			return "DEBUG";
		case 2:
			return "WARNING";
		case 3:
			return "ERROR";
		default:
			return "UNKNOW";
	}
}

//打印日志级别,消息
void LOG(int level_,string message_,string file_ ,int line_)
{
	cout<<"[ "<<GetTimeStamp()<<" ]"<<"[ "<<GetLogLevel(level_)<<" ]"<<"[ "<<file_<<" : "<<line_<<" ]"<<message_<<endl;
}

#define LOG(level_,message_) LOG(level_,message_,__FILE__,__LINE__)

#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拥抱@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值