当前位置: 首页 > wzjs >正文

网站优化方案怎么写电商直播系统开发

网站优化方案怎么写,电商直播系统开发,公司建的是网页还是网站,qq代刷网站推广免费文章目录 生产者消费者模型为何要使用生产者消费者模型生产者消费者模型优点 基于BlockingQueue的生产者消费者模型BlockingQueueC queue模拟阻塞队列的生产消费模型单线程生产消费模型多线程生产消费模型 生产者消费者模型 consumer/productor 321原则(便于记忆) 为何要使用…

文章目录

    • 生产者消费者模型
      • 为何要使用生产者消费者模型
        • 生产者消费者模型优点
      • 基于BlockingQueue的生产者消费者模型
        • BlockingQueue
        • C++ queue模拟阻塞队列的生产消费模型
          • 单线程生产消费模型
          • 多线程生产消费模型

生产者消费者模型

consumer/productor

321原则

321原则(便于记忆)

为何要使用生产者消费者模型

生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题。

生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,

所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,

消费者不找生产者要数据,而是直接从阻塞队列里取,

阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。

这个阻塞队列就是用来给生产者和消费者解耦的。

生产者消费者模型优点
  1. 解耦
  2. 支持并发
  3. 支持忙闲不均

image-20250425234123376

生产消费模型的高效问题

cp问题高效

基于BlockingQueue的生产者消费者模型

BlockingQueue

在多线程编程中阻塞队列(Blocking Queue)是一种常用于实现生产者和消费者模型的数据结构。

其与普通的队列区别在于,

当队列为空时,从队列获取元素的操作将会被阻塞,直到队列中被放入了元素;

当队列满时,往队列里存放元素的操作也会被阻塞,直到有元素被从队列中取出

(以上的操作都是基于不同的线程来说的,线程在对阻塞队列进程操作时会被阻塞)

image-20250425234454539

C++ queue模拟阻塞队列的生产消费模型

代码:

单线程生产消费模型

blockqueue.hpp

#pragma once#include <iostream>
#include <pthread.h>
#include <queue>using namespace std;template <class T>
class blockqueue
{static const int defaultnum = 20;public:blockqueue(int maxcap = defaultnum): maxcap_(maxcap){pthread_mutex_init(&mutex_, nullptr);pthread_cond_init(&c_cond_, nullptr);pthread_cond_init(&p_cond_, nullptr);low_water=maxcap_/3;high_water=maxcap_*2/3;}~blockqueue(){pthread_mutex_destroy(&mutex_);pthread_cond_destroy(&c_cond_);pthread_cond_destroy(&p_cond_);}T pop(){pthread_mutex_lock(&mutex_);if(q_.size()==0){pthread_cond_wait(&c_cond_,&mutex_);//1.调用的时候,自动释放锁}T t=q_.front();             // 你想消费,就直接能消费吗?不一定。你得先确保消费条件满足q_.pop();if(q_.size()<low_water)pthread_cond_signal(&p_cond_);pthread_mutex_unlock(&mutex_);return t;}void push(const T &in){pthread_mutex_lock(&mutex_);if(q_.size()==maxcap_){pthread_cond_wait(&p_cond_,&mutex_);//1.调用的时候,自动释放锁}q_.push(in);                // 你想生产,就直接能生产吗?不一定。你得先确保生产条件满足if(q_.size()>high_water)pthread_cond_signal(&c_cond_);pthread_mutex_unlock(&mutex_);}private:queue<T> q_;int maxcap_;// int mincap_;pthread_mutex_t mutex_;pthread_cond_t c_cond_;pthread_cond_t p_cond_;int low_water;int high_water;
};

main.cc

#include<iostream>
#include"blockqueue.hpp"
#include<unistd.h>using namespace std;void*Consumer(void*args)
{blockqueue<int> *bq=static_cast<blockqueue<int> *>(args);while(1){int data=bq->pop();cout<<"消费了一个数据: "<<data<<endl;// sleep(1);}
}void*Productor(void*args)
{blockqueue<int> *bq=static_cast<blockqueue<int> *>(args);int data=0;while(1){data++;bq->push(data);cout<<"生产了一个数据: "<<data<<endl;sleep(1);}
}int main()
{blockqueue<int> *bq=new blockqueue<int>();pthread_t c,p;pthread_create(&c,nullptr,Consumer,bq);pthread_create(&p,nullptr,Productor,bq);pthread_join(c,nullptr);pthread_join(p,nullptr);delete bq;return 0;
}

image-20250508225919230

消费者sleep

image-20250508230034577

两者都不sleep

image-20250508230244057

多线程生产消费模型

补充:

  1. 为什么判断条件要放到加锁之后?

因为判断临界资源条件是否满足,也是在访问临界资源!

判断临界资源是否就绪,是通过在临界区内部判断的!

  1. 如果临界资源未就绪,那么线程就要进行等待。

等待的时候,线程是持有锁的!所以调用wait时,自动释放锁。

如果不释放锁,直接等待,那么等待的线程就没有线程可以唤醒了,

因为其他线程都在锁外,进不去临界区。

该线程因为唤醒而返回的时候,重新持有锁了。

  1. 如果线程在wait时,被误唤醒了呢?

伪唤醒的概念

伪唤醒

解决方法:判断条件时用while,不用if

blockqueue.hpp

#pragma once#include <iostream>
#include <pthread.h>
#include <queue>using namespace std;template <class T>
class blockqueue
{static const int defaultnum = 20;public:blockqueue(int maxcap = defaultnum): maxcap_(maxcap){pthread_mutex_init(&mutex_, nullptr);pthread_cond_init(&c_cond_, nullptr);pthread_cond_init(&p_cond_, nullptr);// low_water=maxcap_/3;// high_water=maxcap_*2/3;}~blockqueue(){pthread_mutex_destroy(&mutex_);pthread_cond_destroy(&c_cond_);pthread_cond_destroy(&p_cond_);}T pop(){pthread_mutex_lock(&mutex_);while(q_.size()==0)//做到防止代码被伪唤醒!{pthread_cond_wait(&c_cond_,&mutex_);//1.调用的时候,自动释放锁}T t=q_.front();             // 你想消费,就直接能消费吗?不一定。你得先确保消费条件满足q_.pop();// if(q_.size()<low_water)pthread_cond_signal(&p_cond_);pthread_cond_signal(&p_cond_);//pthread_cond_broadcastpthread_mutex_unlock(&mutex_);return t;}void push(const T &in){pthread_mutex_lock(&mutex_);while(q_.size()==maxcap_)//做到防止代码被伪唤醒!{pthread_cond_wait(&p_cond_,&mutex_);//1.调用的时候,自动释放锁}q_.push(in);                // 你想生产,就直接能生产吗?不一定。你得先确保生产条件满足// if(q_.size()>high_water)pthread_cond_signal(&c_cond_);pthread_cond_signal(&c_cond_);pthread_mutex_unlock(&mutex_);}private:queue<T> q_;int maxcap_;// int mincap_;pthread_mutex_t mutex_;pthread_cond_t c_cond_;pthread_cond_t p_cond_;// int low_water;// int high_water;
};

Task.hpp

#pragma once
#include <iostream>
#include<string>
using namespace std;enum
{Div_zero = 1,Mod_zero,Unknown
};class Task
{
public:Task(int x, int y, char op): a(x), b(y), op_(op), ret(0), exitcode(0){}void Run(){switch (op_){case '+':ret = a + b;break;case '-':ret = a - b;break;case '*':ret = a * b;break;case '/':{if (b == 0)exitcode = Div_zero;elseret = a / b;}break;case '%':{if (b == 0)exitcode = Mod_zero;elseret = a % b;}break;default:exitcode=Unknown;break;}}string GetTask(){string r=to_string(a);r+=op_;r+=to_string(b);r+="=???";return r;}string Getret(){string r=to_string(a);r+=op_;r+=to_string(b);r+="=";r+=to_string(ret);r+=" [ exitcode: ";r+=to_string(exitcode);r+=" ]";return r;}void operator()(){Run();}~Task() {}private:int a;int b;char op_;int ret;int exitcode;
};

main.cc

#include <iostream>
#include "blockqueue.hpp"
#include <unistd.h>
#include "Task.hpp"
#include <ctime>using namespace std;string oper = "+-*/%";void *Consumer(void *args)
{blockqueue<Task> *bq = static_cast<blockqueue<Task> *>(args);while (1){Task data = bq->pop();// data.Run();// 计算// data.Run();data();cout << "处理了一个任务 , 运算结果为: " << data.Getret() << " ,thread id: " << pthread_self() << endl;// sleep(1);}
}void *Productor(void *args)
{int len = oper.size();blockqueue<Task> *bq = static_cast<blockqueue<Task> *>(args);// int x=0,y=0;// Task data(x,y);while (1){// 模拟生产者生产数据int x = rand() % 10 + 1; //[1,10]int y = rand() % 10;     //[0,9];char op = oper[rand() % len];Task data(x, y, op);usleep(10);// 计算bq->push(data);cout << "生产了一个任务: " << data.GetTask() << " ,thread id: " << pthread_self() << endl;sleep(1);}
}int main()
{srand(time(nullptr));blockqueue<Task> *bq = new blockqueue<Task>();pthread_t c[3], p[5];for (int i = 0; i < 3; i++){pthread_create(c + i, nullptr, Consumer, bq);}for (int i = 0; i < 5; i++){pthread_create(p + i, nullptr, Productor, bq);}for (int i = 0; i < 3; i++){pthread_join(c[i], nullptr);}for (int i = 0; i < 5; i++){pthread_join(p[i], nullptr);}delete bq;return 0;
}

image-20250511010553986


文章转载自:

http://KB6S9Dg9.hypqh.cn
http://mmcVS74j.hypqh.cn
http://Fr2lMaRR.hypqh.cn
http://o4kmom8N.hypqh.cn
http://Ej9clDsL.hypqh.cn
http://a0mjAZpc.hypqh.cn
http://LQylkDLk.hypqh.cn
http://9Hmjn5Eo.hypqh.cn
http://URZUMwNX.hypqh.cn
http://CZ0YlRQQ.hypqh.cn
http://tLIJ6R2M.hypqh.cn
http://gX4JCQeC.hypqh.cn
http://9C16D2av.hypqh.cn
http://n7g6U2Rd.hypqh.cn
http://Ldlglkpn.hypqh.cn
http://caYJoHGH.hypqh.cn
http://jwK3DKHC.hypqh.cn
http://ZwUsb6EE.hypqh.cn
http://g7y87wSR.hypqh.cn
http://psi7T3Nm.hypqh.cn
http://8ppRFtkn.hypqh.cn
http://k4hlI6Am.hypqh.cn
http://nPwcOlv5.hypqh.cn
http://JC8fWGcC.hypqh.cn
http://JBczgeO9.hypqh.cn
http://o2rOuXCg.hypqh.cn
http://isHcpr1p.hypqh.cn
http://45OkCHBw.hypqh.cn
http://JHoumq67.hypqh.cn
http://K2QJn2Ub.hypqh.cn
http://www.dtcms.com/wzjs/689218.html

相关文章:

  • 九江网站建设公司wordpress 印象码
  • 网站如何用微信支付wordpress用户名和密码
  • 中国网站建设公司百强wordpress 下载站
  • 做app 的模板下载网站有哪些百度字体如何转换wordpress
  • 宜宾市城乡建设厅网站免费试用网站空间
  • 厦门找一家做网站的公司汽车专业网站
  • wordpress主题qux如何做网站优化推广
  • 做纸巾定制的网站我做微信淘宝客网站
  • 东莞市国外网站建设多少钱修改 wordpress footer
  • 网站开发一个人可以完成吗做led灯网站有哪些呢
  • 知名的教育行业网站开发ui自学网站
  • 俄语网站上海市建设工程招标公告
  • flash网站模板源码专业网站设计制作过程
  • 企业网站怎么管理系统招生处网站建设方案
  • 如何利用微信进行企业网站推广兴义网站建设网站建设
  • 做互联网网站需要什么资质吗鞍山网站制作推广
  • 网站是怎么建成的公司网站上线的通知
  • 做地方黄页网站外链代发公司
  • 电商企业门户网站建设方案国际军事新闻最新消息今天
  • dede网站模板页在什么文件夹自己做模板网站
  • 常用外贸网站企业网站建设的缺点
  • 网站打不开了怎么办商城小程序定制公司
  • 建设工程学部研究生培养网站商洛网站开发公司
  • 高档网站建设公司广州英文建站公司
  • 网站支付宝怎么做的数字广东网络建设有限公司地址
  • 做网站定制开发的公司哪家好济南快速网站排名
  • 网站开发+进度表做互联网产品和运营必备的网站
  • 张家港手机网站制作教育行业展示网站模板
  • 东莞正规的企业网站设计多少钱wordpress 标题长度 省略号
  • 做兼职一般去哪个网站好无锡网站的优化哪家好