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

纸做的花朵成品网站中国企业公示信息网官网

纸做的花朵成品网站,中国企业公示信息网官网,软件开发需要用什么软件,asp做登入网站信号量 信号量的本质是一个计数器#xff0c;可以用来衡量临界资源中资源数量多少 信号量的PV操作 P操作#xff1a;申请信号量称为P操作#xff0c;P操作的本质就是让计数器减1。 V操作#xff1a;释放信号量称为V操作#xff0c;V操作的本质就是让计数器加1 POSIX信号量…信号量 信号量的本质是一个计数器可以用来衡量临界资源中资源数量多少 信号量的PV操作 P操作申请信号量称为P操作P操作的本质就是让计数器减1。 V操作释放信号量称为V操作V操作的本质就是让计数器加1 POSIX信号量相关的接口函数 初始化信号量 int sem_init(sem_t *sem, int pshared, unsigned int value); sem需要初始化的信号量。 pshared0表示线程间共享非0表示进程间共享。 value信号量的初始值特定资源的初始数量。 等待信号量P操作) int sem_wait(sem_t *sem); 发布信号量V操作) int sem_post(sem_t *sem); 销毁信号量 int sem_destroy(sem_t *sem); 基于环形队列的生产消费模型代码 RingQueue.hpp #pragma once#include iostream #include vector #include cassert #include semaphore.h #include pthread.hstatic const int gcap 5;templateclass T class RingQueue { private:void P(sem_t sem){int n sem_wait(sem);assert(n 0); // if(void)n;}void V(sem_t sem){int n sem_post(sem);assert(n 0);(void)n;} public:RingQueue(const int cap gcap): _queue(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;pthread_mutex_init(_pmutex, nullptr);pthread_mutex_init(_cmutex, nullptr);}// 生产者void Push(const T in){//先申请信号量在加锁P(_spaceSem); pthread_mutex_lock(_pmutex); _queue[_productorStep] in;_productorStep % _cap;pthread_mutex_unlock(_pmutex);V(_dataSem);}// 消费者void Pop(T *out){//先申请信号量在加锁P(_dataSem);pthread_mutex_lock(_cmutex);*out _queue[_consumerStep];_consumerStep % _cap;pthread_mutex_unlock(_cmutex);V(_spaceSem);}~RingQueue(){sem_destroy(_spaceSem);sem_destroy(_dataSem);pthread_mutex_destroy(_pmutex);pthread_mutex_destroy(_cmutex);} private:std::vectorT _queue;int _cap;sem_t _spaceSem; // 生产者的空间资源sem_t _dataSem; // 消费者的数据资源int _productorStep;int _consumerStep;pthread_mutex_t _pmutex;pthread_mutex_t _cmutex; }; Task.hpp  #pragma once#include iostream #include string #include cstdio #include functionalclass Task {using func_t std::functionint(int,int,char);// typedef std::functionint(int,int) func_t; public:Task(){}Task(int x, int y, char op, func_t func):_x(x), _y(y), _op(op), _callback(func){}std::string operator()(){int result _callback(_x, _y, _op);char buffer[1024];snprintf(buffer, sizeof buffer, %d %c %d %d, _x, _op, _y, result);return buffer;}std::string toTaskString(){char buffer[1024];snprintf(buffer, sizeof buffer, %d %c %d ?, _x, _op, _y);return buffer;} private:int _x;int _y;char _op;func_t _callback; };const std::string oper -*/%;int mymath(int x, int y, char op) {int result 0;switch (op){case :result x y;break;case -:result x - y;break;case *:result x * y;break;case /:{if (y 0){std::cerr div zero error! std::endl;result -1;}elseresult x / y;}break;case %:{if (y 0){std::cerr mod zero error! std::endl;result -1;}elseresult x % y;}break;default:// do nothingbreak;}return result; } main.cc #include RingQueue.hpp #include Task.hpp #include pthread.h #include ctime #include cstdlib #include sys/types.h #include unistd.hstd::string SelfName() {char name[128];snprintf(name, sizeof(name), thread[0x%x], pthread_self());return name; }void *ProductorRoutine(void *rq) {RingQueueTask *ringqueue static_castRingQueueTask *(rq);while(true){int x rand() % 10;int y rand() % 5;char op oper[rand()%oper.size()];Task t(x, y, op, mymath);// 生产任务ringqueue-Push(t);// 输出提示std::cout SelfName() , 生产者派发了一个任务: t.toTaskString() std::endl;sleep(1);} }void *ConsumerRoutine(void *rq) {RingQueueTask *ringqueue static_castRingQueueTask *(rq);while(true){Task t;//消费任务ringqueue-Pop(t);std::string result t(); std::cout SelfName() , 消费者消费了一个任务: result std::endl;} }int main() {srand((unsigned int)time(nullptr) ^ getpid() ^ pthread_self() ^ 0x71727374);RingQueueTask *rq new RingQueueTask();pthread_t p[4], c[8];for(int i 0; i 4; i) pthread_create(pi, nullptr, ProductorRoutine, rq);for(int i 0; i 8; i) pthread_create(ci, nullptr, ConsumerRoutine, rq);for(int i 0; i 4; i) pthread_join(p[i], nullptr);for(int i 0; i 8; i) pthread_join(c[i], nullptr);delete rq;return 0; } 测试结果
http://www.pierceye.com/news/177300/

相关文章:

  • 零食店网站构建策划报告高级程序员培训
  • 重庆大足网站制作公司百度app智能小程序
  • flash网站与html5discuz做的网站上传到网站空间的文件
  • 做网站会什么网页设计类型与风格
  • 怎么做网站用于推广注册公司每年需要缴纳什么费用
  • 揭阳有哪家网站制作公司wordpress数据库备份恢复
  • 站长工具友链查询中国网站建设公司图片
  • 做原型的素材网站国内wordpress主题商
  • 合肥的电商网站设计wordpress 相册 链接
  • 试玩平台网站怎么做网站建设推荐中企动力
  • 衡水做网站建设台州网站建设选浙江华企
  • 某集团网站建设规划书用flash做的经典网站
  • 企业网站用什么做一个空间怎么放两个网站吗
  • 58同城长沙回收网站建设长春seo推广
  • 景区网站建设的意义女生学计算机应用技术可以做什么
  • 做律师网站的公司天津公司网站制作
  • 上海建设摩托车官方网站招聘网站数建设
  • 自己制作一个网站需要什么软件安吉网站制作
  • 如何设计服装网站首页网站建设比较好的公司
  • 微信网站的链接标志图片如何做公众号如何创建
  • 建站公司建的网站能改动吗怎样设置默认网站
  • 高并发电商网站开发辽宁省朝阳市做网站
  • 公司做网站有用吗合肥企业快速建站
  • 因脉网站建设公司怎么呀韩国网站开发建设技术特点
  • 网站备案 个人 单位安徽网站建设推广
  • 黔江网站制作网站招标建设
  • 网站注册公司目前最好的引流方法
  • 小米wifi设置网址入口网站通信工程建设网站
  • 深圳专业网站建在线做插画的网站
  • 站长之家网站查询怎么样创建自己的小程序