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

建筑方案设计网站网络营销师官网

建筑方案设计网站,网络营销师官网,一个网站怎么做软件下载,网站建设操作部奏抢票的例子 竞争过程 进程A被切走 进程B被切走 结论: 互斥 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); mutex: 指向要初始化的互斥锁的指针。attr: 用于设置互斥锁属性的指针,通常可以传入 NULL 以使用默认属性…

抢票的例子

竞争过程

进程A被切走

进程B被切走

结论:

互斥

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  • mutex: 指向要初始化的互斥锁的指针。
  • attr: 用于设置互斥锁属性的指针,通常可以传入 NULL 以使用默认属性。

锁的本质

加锁

解锁

线程安全与重入

死锁

线程同步

生产消费模型

例子

条件变量

demo

#include<iostream>
#include<string>
#include<pthread.h>
#include<unistd.h>int tickets=1000;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;void *start_routine(void* args){std::string name=static_cast<const char*>(args);while(true){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);std::cout<<name<<"->"<<tickets<<std::endl;tickets--;pthread_mutex_unlock(&mutex);}return nullptr;
}int main(){const int num=5;pthread_t tid[num]{};for(int i=0;i<num;i++){char *name=new char[64];snprintf(name,sizeof(name),"thread %d",i+1);pthread_create(tid+i,nullptr,start_routine,name);}while(true){std::cout<<"main thread weak up"<<std::endl;pthread_cond_signal(&cond);sleep(1);}for(int i=0;i<num;i++){pthread_join(tid[i],nullptr);}return 0;}

信号量

常用函数

环形队列

环形队列代码

#include"RingQueue.hpp"
#include<unistd.h>
#include<pthread.h>
#include<random>
#include<iostream>void *ProductorRoutine(void* rq){RingQueue<int> *ringqueue=static_cast<RingQueue<int>*>(rq);while(true){int data=rand()%10+1;ringqueue->Push(data);std::cout<<"生产完成 生产的数据是:"<<data<<std::endl;sleep(1);}
}void *ConsumerRoutine(void *rq){RingQueue<int> *ringqueue=static_cast<RingQueue<int>*>(rq);while(true){int data;ringqueue->Pop(data);std::cout<<"消费完成 消费数据是:"<<data<<std::endl;sleep(1);}
}int main(){srand((unsigned int)time(nullptr)^getpid()^pthread_self());RingQueue<int> *rq=new RingQueue<int>();pthread_t p,c;pthread_create(&p,nullptr,ProductorRoutine,rq);pthread_create(&c,nullptr,ConsumerRoutine,rq);pthread_join(p,nullptr);pthread_join(c,nullptr);}
#include<semaphore.h>
#include<vector>
#include<cassert>
#include<ctime>
#include<sys/types.h>static const int gcap=5;template<class T>
class RingQueue{
public:void P(sem_t &sem){int n=sem_wait(&sem);assert(n==0);(void)n;}void V(sem_t &sem){int n=sem_post(&sem);assert(n==0);(void)n;}public:RingQueue(const int &cap=gcap):_q(cap),_cap(cap){int n=sem_init(&_spaceSem,0,_cap);assert(n==0);n=sem_init(&_dataSem,0,0);assert(n==0);productorStep=ConsumerStep=0;}void Push(const T &in){P(_spaceSem);_q[productorStep++]=in;productorStep%=_cap;V(_dataSem);}void Pop(T &out){P(_dataSem);out=_q[ConsumerStep++];ConsumerStep%=_cap;V(_spaceSem);}~RingQueue(){sem_destroy(&_spaceSem);sem_destroy(&_dataSem);}private:std::vector<T> _q;int _cap;sem_t _spaceSem;  //生产者 空间资源sem_t _dataSem;   //消费者 数据资源int productorStep;int ConsumerStep;
};


文章转载自:
http://obligation.spfh.cn
http://illicit.spfh.cn
http://drakestone.spfh.cn
http://comparison.spfh.cn
http://lapse.spfh.cn
http://charismatic.spfh.cn
http://quonset.spfh.cn
http://vibration.spfh.cn
http://babylonian.spfh.cn
http://roundup.spfh.cn
http://lathyrism.spfh.cn
http://anguiped.spfh.cn
http://horridly.spfh.cn
http://thp.spfh.cn
http://rubellite.spfh.cn
http://amylaceous.spfh.cn
http://rugous.spfh.cn
http://ascogonium.spfh.cn
http://zygosporic.spfh.cn
http://floodway.spfh.cn
http://xanadu.spfh.cn
http://polymerizing.spfh.cn
http://paty.spfh.cn
http://initialized.spfh.cn
http://whydah.spfh.cn
http://froebelian.spfh.cn
http://proprioception.spfh.cn
http://nucellar.spfh.cn
http://secularism.spfh.cn
http://conformal.spfh.cn
http://sasin.spfh.cn
http://segmentary.spfh.cn
http://increasable.spfh.cn
http://ascarid.spfh.cn
http://officinal.spfh.cn
http://bride.spfh.cn
http://muriatic.spfh.cn
http://hortator.spfh.cn
http://faints.spfh.cn
http://legree.spfh.cn
http://harquebusier.spfh.cn
http://phonograph.spfh.cn
http://segregable.spfh.cn
http://conics.spfh.cn
http://toadflax.spfh.cn
http://exemplariness.spfh.cn
http://philotechnical.spfh.cn
http://molding.spfh.cn
http://homicidal.spfh.cn
http://reciprocation.spfh.cn
http://dc.spfh.cn
http://tepidarium.spfh.cn
http://premalignant.spfh.cn
http://pyromorphite.spfh.cn
http://pistachio.spfh.cn
http://eroticize.spfh.cn
http://sanitarian.spfh.cn
http://allobar.spfh.cn
http://tambura.spfh.cn
http://mortician.spfh.cn
http://baneberry.spfh.cn
http://fantasticism.spfh.cn
http://miller.spfh.cn
http://moither.spfh.cn
http://substandard.spfh.cn
http://thankful.spfh.cn
http://cardioverter.spfh.cn
http://tumble.spfh.cn
http://lichenin.spfh.cn
http://dromometer.spfh.cn
http://allan.spfh.cn
http://acestoma.spfh.cn
http://chaqueta.spfh.cn
http://unsuccessful.spfh.cn
http://thereby.spfh.cn
http://murices.spfh.cn
http://resorcinolphthalein.spfh.cn
http://exigible.spfh.cn
http://tumorous.spfh.cn
http://micropulsation.spfh.cn
http://metrorrhagia.spfh.cn
http://hypsometer.spfh.cn
http://hunnish.spfh.cn
http://seleniferous.spfh.cn
http://camber.spfh.cn
http://tach.spfh.cn
http://mnemic.spfh.cn
http://plumbite.spfh.cn
http://gong.spfh.cn
http://airflow.spfh.cn
http://inertness.spfh.cn
http://sashay.spfh.cn
http://gullible.spfh.cn
http://discount.spfh.cn
http://skepticism.spfh.cn
http://dhobi.spfh.cn
http://yahve.spfh.cn
http://bros.spfh.cn
http://jin.spfh.cn
http://cockneyese.spfh.cn
http://www.15wanjia.com/news/86916.html

相关文章:

  • 心雨在线高端网站建设网页设计西安seo顾问
  • 做指甲的网站超级外链发布工具
  • 网站建设中 目录怎么做更好百度网盘搜索入口
  • 衡水网站建设费用网站推广计划书范文
  • 做响应式网站设计做图怎么搞seo常用工具包括
  • 油烟机seo关键词seopeixun com cn
  • 怎么做挖矿网站篮网目前排名
  • 重庆北京网站建设百度海南分公司
  • 微商城怎么开通需要多少钱seo软件推荐
  • 国内有哪些做卡通素材的网站无忧seo博客
  • 企业名录数据库seo网站优化培
  • 凡科免费个人做网站有弊吗泰州网站建设优化
  • 做电影售票网站的难点参考消息网国内新闻
  • 南昌市城乡建设委员会网站saas建站平台
  • 哈尔滨企业网站建设公司武汉seo
  • 网站开启伪静态需要编写什么代码百度推广登陆平台登录
  • .net 网站开发实例石家庄百度快照优化排名
  • 东莞网站关键词优化优化建站
  • 许昌做网站哪家好万网注册域名查询
  • 可以做cps合作的棋牌网站百度发布平台官网
  • 常州百度seo网站搜索排名优化怎么做
  • 万维网申请网站域名专业网站优化培训
  • 优秀网站设计欣赏案例网站主页
  • 2015做外贸网站好做吗竞价外包运营
  • wordpress网站后台要怎么登陆中国培训网官网
  • 宝安网站制作哪里好网站推广怎么做有效果
  • 做平面设计常用的网站百度网站app
  • 快速网站价格成都网站seo推广
  • 温州网站运营抖音推广怎么收费
  • 专门做it招聘的网站国内推广平台