Linux__线程池及设计模式(单例模式)
Posted Y—X
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux__线程池及设计模式(单例模式)相关的知识,希望对你有一定的参考价值。
文章目录
线程池 :
线程池=线程安全队列+一大堆的线程(执行任务的线程)
线程池示例:
- 创建固定数量线程池,循环从任务队列中获取任务对象,
- 获取到任务对象后,执行任务对象中的任务接口
代码示例:
#pragma once
#include <unistd.h>
#include <math.h>
#include <pthread.h>
#include <iostream>
#include <queue>
#define NUM 5
class Task
public:
int base;
public:
Task()
Task(int _b)
:base(_b)
void Run()
std::cout << "thread is[" << pthread_self() << "] task run ... done: base# " << base << "pow is# " << pow(base, 2) << std::endl;
~Task()
;
class ThreadPool
private:
std::queue<Task*> q;
int max_num;//线程总数
pthread_mutex_t lock;
pthread_cond_t cond;//让consumer等待
public:
void LockQueue()
pthread_mutex_lock(&lock);
void UnlockQueue()
pthread_mutex_unlock(&lock);
bool IsEmpty()
return q.size() == 0;
void ThreadWait()
pthread_cond_wait(&cond, &lock);
void ThreadWakeUp()
pthread_cond_signal(&cond);//指定唤醒
void ThreadsWakeUP()
pthread_cond_broadcast(&cond);//唤醒所有
public:
ThreadPool(int _max=NUM)
:max_num(_max)
static void *Routine(void *arg)//内部成员函数带this指针,回调函数一个参数
ThreadPool *this_p = (ThreadPool*)arg;
while(true)
this_p->LockQueue();
while(this_p->IsEmpty())//没任务就一直等待
this_p->ThreadWait();
Task t;
this_p->Get(t);//取任务
this_p->UnlockQueue();
t.Run();//执行任务在释放锁后
void ThreadPoolInit()
pthread_mutex_init(&lock, nullptr);
pthread_cond_init(&cond, nullptr);
pthread_t t;
for(int i = 0; i < max_num; ++i)
pthread_create(&t, nullptr, Routine, this);
//外部server放任务
void Put(Task &in)
LockQueue();
q.push(&in);//塞任务
UnlockQueue();
ThreadWakeUp();
//内部ThreadPool执行任务
void Get(Task &out)
Task *t = q.front();
q.pop();
out = *t;
~ThreadPool()
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
;
#include "ThreadPool.hpp"
int main()
ThreadPool *tp = new ThreadPool();
tp->ThreadPoolInit();
//server一直构造任务
while(true)
int x = rand()%10+1;
Task t(x);
tp->Put(t);
sleep(1);
return 0;
线程池存在的价值:
- 有任务,立马有线程进行服务,省掉了线程创建的时间。
- 有效防止server中线程过多,导致系统过载的问题。
- 可以避免在峰值压力下,系统资源耗尽的风险;并且可以统一对线程池中的线程进行管理,调度监控。
线程池vs进程池:
- 线程池占用资源更少,但鲁棒性不强。
- 进程池占用资源更多,但鲁棒性强。
单例模式
定义:确保一个类最多只有一个实例,并提供一个全局访问点
单例模式可以分为两种:预加载和延时加载
饿汉模式
预加载,再进一步解释就是还没有使用该单例对象,但是,该单例对象就已经被加载到内存了。
template<class T>
class singlen
public:
//禁止构造,禁止拷贝构造,禁止拷贝
static T* GetInstance()
return &data_;
private:
static T data;
;
优点:程序运行速度很快;缺点:程序初始化的时候就耗时比较长。
懒汉模式–延时加载
资源在使用的时候才进行实例化,单例列的对象在使用的时候才进行实例化。
class singten
public:
static singten* GetInstance();
private:
signlen();
volatile static singten* p;
;
static singten* GetInstance()
if(p==NULL)
pthread_mutex_lock(&locK_);
if(p==NULL)
p=new singten();
pthread_mutex_unlock(&lock_);
return p;
- 互斥锁保证线程安全
- 双重if判空,降低锁竞争的概率
- volatile关键字防止过度优化
以上是关于Linux__线程池及设计模式(单例模式)的主要内容,如果未能解决你的问题,请参考以下文章