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

dig网站开发软件培训班

dig网站开发,软件培训班,seo管理系统创作,wordpress导入项目文章目录 一.条件变量pthread线程库提供的条件变量操作 二.生产者消费者模型生产者消费者模型的高效性基于环形队列实现生产者消费者模型中的数据容器 一.条件变量 条件变量是线程间共享的全局变量,线程间可以通过条件变量进行同步控制条件变量的使用必须依赖于互斥锁以确保线…

在这里插入图片描述

文章目录

  • 一.条件变量
    • pthread线程库提供的条件变量操作
  • 二.生产者消费者模型
    • 生产者消费者模型的高效性
    • 基于环形队列实现生产者消费者模型中的数据容器

一.条件变量

  • 条件变量是线程间共享的全局变量,线程间可以通过条件变量进行同步控制
  • 条件变量的使用必须依赖于互斥锁以确保线程安全,线程申请了互斥锁后,可以调用特定函数进入条件变量等待队列(同时释放互斥锁),其他线程则可以通过条件变量在特定的条件下唤醒该线程(唤醒后线程重新获得互斥锁),实现线程同步.
    • 例如一个线程访问队列时,发现队列为空,则它只能等待其它线程将数据添加到队列中,这种情况就需要用到条件变量.
    • 线程同步的概念:在保证数据安全的前提下,让线程能够按照某种特定的顺序访问共享资源,从而有效避免线程饥饿问题(饥饿问题指线程长时间等待资源而无法被调度).
      在这里插入图片描述

pthread线程库提供的条件变量操作

//声明全局互斥锁并初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//声明全局条件变量并初始化
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  • 线程等待条件:
任务线程代码{pthread_mutex_lock(&mutex);if(条件为假)pthread_cond_wait(&cond, &mutex);//等待时会释放互斥锁,等待完后自动加锁//访问共享资源....pthread_mutex_unlock(&mutex);
}

线程调用pthread_cond_wait等待时,该接口会释放互斥锁,等待结束后自动加锁

  • 控制线程给条件变量发送唤醒信号
控制线程代码{if(满足唤醒条件){pthread_mutex_lock(&mutex);pthread_cond_signal(cond);pthread_mutex_unlock(&mutex);}
}

唤醒操作加锁是为了避免信号丢失

  • 示例:
#include <iostream>
#include <unistd.h>
#include <pthread.h>int cnt = 0;
//声明全局互斥锁并初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//声明全局条件变量并初始化
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;void *Count(void * args)
{//线程分离,无需主线程等待pthread_detach(pthread_self());uint64_t number = (uint64_t)args;std::cout << "pthread: " << number << " create success" << std::endl;while(true){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);               std::cout << "pthread: " << number << " , cnt: " << cnt++ << std::endl;pthread_mutex_unlock(&mutex);}
}int main()
{for(uint64_t i = 0; i < 4; i++){pthread_t tid;pthread_create(&tid, nullptr, Count, (void*)i);usleep(1000);}sleep(3);std::cout << "main thread ctrl begin: " << std::endl;while(true) {sleep(1);//唤醒在cond的等待队列中等待的一个线程,默认都是第一个pthread_mutex_lock(&mutex);pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex);//按顺序唤醒在cond的等待队列中的所有线程//pthread_cond_broadcast(&cond);std::cout << "signal one thread..." << std::endl;}return 0;
}
  • 线程同步过程图解:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 条件变量和锁的销毁:
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);

二.生产者消费者模型

  • 生产者消费者模型是一种多线程并发协作的设计框架,生产者负责生成并发送数据,消费者负责接收并处理数据.
  • 生产者和消费者之间存在一个数据容器作为缓冲区,生产者生产的数据存入容器中,消费者需要的数据从容器中获取,实现了生产者和消费者之间的数据传输解耦
  • 数据容器由互斥锁保护,同一个时刻只能有一个线程访问数据容器,生产者和消费者之间通过条件变量(或信号量)实现同步
  • 对于数据容器的访问,生产者和消费者遵循三个原则:
    • 生产者和生产者之间互斥
    • 消费者和消费者之间互斥
    • 生产者和消费者之间互斥并同步
      在这里插入图片描述

生产者消费者模型的高效性

  • 由于生产者和消费者之间的数据传输解耦,生产者生产完数据之后不用等待消费者处理数据,而是直接将数据存入容器,消费者不需要向生产者请求数据,而是直接从容器里获取数据,因此即便在生产者和消费者的效率不对等且多变的情况下,多个生产者依然可以高效专一地并发生产数据,多个消费者依然可以高效专一地并发处理数据,使得系统整体的并发量得到提高
    在这里插入图片描述

基于环形队列实现生产者消费者模型中的数据容器

  • 环形队列中,消费者访问队列的头指针进行数据出队操作,生产者访问队列的尾指针进行数据入队操作
  • 两把互斥锁分别保证消费者和消费者之间的互斥以及生产者和生产者之间的互斥,两个信号量实现消费者和生产者之间的互斥与同步
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述

  • 当环形队列既不为空也不为满时,支持一个生产者和一个消费者并发地进行数据的存取
#pragma once
#include <iostream>
#include <vector>
#include <semaphore.h>
#include <pthread.h>//环形队列默认容量
const static int defaultcap = 5;template<class T>
class RingQueue{
private:std::vector<T> ringqueue_;int cap_;          //容器的容量int c_step_;       // 消费者环形队列指针int p_step_;       // 生产者环形队列指针sem_t cdata_sem_;  // 消费者的数据资源sem_t pspace_sem_; // 生产者的空间资源pthread_mutex_t c_mutex_;   //消费者与消费者之间的互斥锁pthread_mutex_t p_mutex_;   //生产者与生产者之间的互斥锁
public:RingQueue(int cap = defaultcap):ringqueue_(cap), cap_(cap), c_step_(0), p_step_(0){//初始化生产者和消费者的信号量-->消费者一开始没有信号量资源,生产者一开始具有最多的空间资源sem_init(&cdata_sem_, 0, 0);sem_init(&pspace_sem_, 0, cap);pthread_mutex_init(&c_mutex_, nullptr);pthread_mutex_init(&p_mutex_, nullptr);}~RingQueue(){sem_destroy(&cdata_sem_);sem_destroy(&pspace_sem_);pthread_mutex_destroy(&c_mutex_);pthread_mutex_destroy(&p_mutex_);}//信号量的资源状态可以区分队列的空和满void Push(const T &in) {//生产者等待空间资源sem_wait(&pspace_sem_);pthread_mutex_lock(&p_mutex_);ringqueue_[p_step_] = in;p_step_++;p_step_ %= cap_;pthread_mutex_unlock(&p_mutex_);//生产完数据后增加消费者的信号量资源sem_post(&cdata_sem_);}void Pop(T *out)      {//消费者等待数据资源sem_wait(&cdata_sem_);pthread_mutex_lock(&c_mutex_);*out = ringqueue_[c_step_];c_step_++;c_step_ %= cap_;pthread_mutex_unlock(&c_mutex_);//消费完数据后增加生产者的信号量资源sem_post(&pspace_sem_);}
};

在这里插入图片描述


文章转载自:
http://maduro.bfmq.cn
http://congenitally.bfmq.cn
http://fagmaster.bfmq.cn
http://spoonerism.bfmq.cn
http://pigmentize.bfmq.cn
http://ratisbon.bfmq.cn
http://nepheline.bfmq.cn
http://ingeminate.bfmq.cn
http://frequency.bfmq.cn
http://slingman.bfmq.cn
http://picture.bfmq.cn
http://parylene.bfmq.cn
http://mahabharata.bfmq.cn
http://isodynamic.bfmq.cn
http://misspeak.bfmq.cn
http://disbandment.bfmq.cn
http://rembrandtesque.bfmq.cn
http://patagium.bfmq.cn
http://stopover.bfmq.cn
http://toboggan.bfmq.cn
http://contrabandage.bfmq.cn
http://preservatize.bfmq.cn
http://pdf.bfmq.cn
http://pressor.bfmq.cn
http://filariasis.bfmq.cn
http://sluice.bfmq.cn
http://gaza.bfmq.cn
http://tiresome.bfmq.cn
http://trochilus.bfmq.cn
http://aponeurosis.bfmq.cn
http://radiate.bfmq.cn
http://superordinary.bfmq.cn
http://athena.bfmq.cn
http://chainstitch.bfmq.cn
http://depletive.bfmq.cn
http://velodyne.bfmq.cn
http://faggoty.bfmq.cn
http://unconventional.bfmq.cn
http://impanel.bfmq.cn
http://nylex.bfmq.cn
http://unitage.bfmq.cn
http://paedagogue.bfmq.cn
http://unreal.bfmq.cn
http://florin.bfmq.cn
http://propylite.bfmq.cn
http://raft.bfmq.cn
http://inextricable.bfmq.cn
http://justice.bfmq.cn
http://enchiridion.bfmq.cn
http://monolithic.bfmq.cn
http://answerable.bfmq.cn
http://bicone.bfmq.cn
http://discretization.bfmq.cn
http://opportunism.bfmq.cn
http://sheriffwick.bfmq.cn
http://vinometer.bfmq.cn
http://fao.bfmq.cn
http://repercussive.bfmq.cn
http://diagnose.bfmq.cn
http://viticultural.bfmq.cn
http://pricewise.bfmq.cn
http://ent.bfmq.cn
http://bepowder.bfmq.cn
http://hertha.bfmq.cn
http://catacomb.bfmq.cn
http://detoxicator.bfmq.cn
http://inconvincible.bfmq.cn
http://evangelically.bfmq.cn
http://appentice.bfmq.cn
http://bothie.bfmq.cn
http://premiate.bfmq.cn
http://crushable.bfmq.cn
http://emote.bfmq.cn
http://nondense.bfmq.cn
http://jig.bfmq.cn
http://badderlocks.bfmq.cn
http://snowshed.bfmq.cn
http://turgite.bfmq.cn
http://homunculus.bfmq.cn
http://hyperkinesis.bfmq.cn
http://miscellanist.bfmq.cn
http://notarization.bfmq.cn
http://catabatic.bfmq.cn
http://intimation.bfmq.cn
http://helper.bfmq.cn
http://maidenhood.bfmq.cn
http://headstall.bfmq.cn
http://arteriogram.bfmq.cn
http://uncredited.bfmq.cn
http://proser.bfmq.cn
http://imponent.bfmq.cn
http://ostracoderm.bfmq.cn
http://matildawaltzer.bfmq.cn
http://souwester.bfmq.cn
http://backswordman.bfmq.cn
http://soapstone.bfmq.cn
http://presentiment.bfmq.cn
http://hopple.bfmq.cn
http://working.bfmq.cn
http://conversible.bfmq.cn
http://www.dt0577.cn/news/125337.html

相关文章:

  • dedecms 购物网站360手机优化大师安卓版
  • 徐州做外贸网站小广告模板
  • 体育新闻最新消息乒乓球seo优化工具大全
  • 常熟网站制作今日最新国内新闻
  • 厦门网站建设屈兴东百度广告怎么做
  • 网站建设的认识网络营销的基本功能
  • 建设网站虚拟主机百度浏览器
  • 南宁电子推广网站旺道seo推广效果怎么样
  • 免费网站建设c3sales制作网页的网站
  • 虎门有没有做网站公司百度推广销售员的工作内容
  • 网站推广方案途径市场营销最有效的手段
  • 网站设置怎么删除网络推广推广培训
  • 淮安市建设工程质量监督站网站网站数据查询
  • 部门子网站建设方案发布外链
  • wordpress动态sidebar青岛网络优化费用
  • 如何利用ps做网站软文范文大全1000字
  • oa办公系统网站开发引流最好的推广方法
  • wordpress为什么加载速度很慢2022年seo还值得做吗
  • 网站建设开发客户真正免费建站
  • 任丘市网站建设百度指数疫情
  • 专业的建网站的公司英语培训机构前十名
  • 深圳个人做网站网页制作网站
  • 基于web的网站建设步骤专业seo公司
  • 广西住房与城乡建设厅网站首页企业营销策划实训报告
  • c2c网站怎么做做市场推广应该掌握什么技巧
  • 方向专业网站制作咨询最近韩国电影片
  • 大淘客平台怎么做分销网站华联股份股票
  • 做网站在厦门排前5名宁波谷歌优化
  • 米思米网站订单取消怎么做东莞好的网站国外站建设价格
  • 电子工程网站外贸网站平台有哪些