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

网站建设项目内控单电子商务网站建设的步骤

网站建设项目内控单,电子商务网站建设的步骤,wordpress插件漏洞扫描,小程序问答库条件变量#xff1a; 条件变量本身不是锁#xff01;但它也可以造成线程阻塞。通常与互斥锁配合使用。给多线程提供一个会合的场所。 主要应用函数#xff1a; pthread_cond_init函数 pthread_cond_destroy函数 pthread_cond_wait函数 pthread_cond_timedwait函数 pthread_c… 条件变量        条件变量本身不是锁但它也可以造成线程阻塞。通常与互斥锁配合使用。给多线程提供一个会合的场所。 主要应用函数        pthread_cond_init函数        pthread_cond_destroy函数        pthread_cond_wait函数        pthread_cond_timedwait函数        pthread_cond_signal函数        pthread_cond_broadcast函数 以上6 个函数的返回值都是成功返回0 失败直接返回错误号。        pthread_cond_t类型   用于定义条件变量        pthread_cond_t cond; pthread_cond_init函数 初始化一个条件变量 int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);       参2attr表条件变量属性通常为默认值传NULL即可 也可以使用静态初始化的方法初始化条件变量 pthread_cond_t cond PTHREAD_COND_INITIALIZER; pthread_cond_destroy函数 销毁一个条件变量 int pthread_cond_destroy(pthread_cond_t *cond); pthread_cond_wait函数 阻塞等待一个条件变量     int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); 函数作用 阻塞等待条件变量cond参1满足释放已掌握的互斥锁解锁互斥量相当于pthread_mutex_unlock(mutex); 1.2.两步为一个原子操作。 当被唤醒pthread_cond_wait函数返回时解除阻塞并重新申请获取互斥锁pthread_mutex_lock(mutex);pthread_cond_timedwait函数 限时等待一个条件变量 int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);        参3    参看man sem_timedwait函数查看struct timespec结构体。               struct timespec {                      time_t tv_sec;        /* seconds */ 秒                      long   tv_nsec;    /* nanosecondes*/ 纳秒               }                                                       形参abstime绝对时间。                                                                如time(NULL)返回的就是绝对时间。而alarm(1)是相对时间相对当前时间定时1秒钟。                      struct timespec t {1, 0};                      pthread_cond_timedwait (cond, mutex, t); 只能定时到 1970年1月1日 00:00:01秒(早已经过去)               正确用法                      time_t cur time(NULL); 获取当前时间。 struct timespec t;  定义timespec 结构体变量t                      t.tv_sec cur1; 定时1秒 pthread_cond_timedwait (cond, mutex, t); 传参                          参APUE.11.6线程同步条件变量小节               在讲解setitimer函数时我们还提到另外一种时间类型         struct timeval {              time_t      tv_sec;  /* seconds */ 秒              suseconds_t tv_usec; /* microseconds */ 微秒         }; pthread_cond_signal函数 唤醒至少一个阻塞在条件变量上的线程 int pthread_cond_signal(pthread_cond_t *cond); pthread_cond_broadcast函数 唤醒全部阻塞在条件变量上的线程     int pthread_cond_broadcast(pthread_cond_t *cond); 生产者消费者条件变量模型 线程同步典型的案例即为生产者消费者模型而借助条件变量来实现这一模型是比较常见的一种方法。假定有两个线程一个模拟生产者行为一个模拟消费者行为。两个线程同时操作一个共享资源一般称之为汇聚生产向其中添加产品消费者从中消费掉产品。 看如下示例使用条件变量模拟生产者、消费者问题 #include stdlib.h #include unistd.h #include pthread.hstruct msg {struct msg *next;int num; }; struct msg *head;pthread_cond_t has_product PTHREAD_COND_INITIALIZER; pthread_mutex_t lock PTHREAD_MUTEX_INITIALIZER;void *consumer(void *p) {struct msg *mp;for (;;) {pthread_mutex_lock(lock);while (head NULL) { //头指针为空,说明没有节点 可以为if吗pthread_cond_wait(has_product, lock);}mp head; head mp-next; //模拟消费掉一个产品pthread_mutex_unlock(lock);printf(-Consume ---%d\n, mp-num);free(mp);sleep(rand() % 5);} } void *producer(void *p) {struct msg *mp;while (1) {mp malloc(sizeof(struct msg));mp-num rand() % 1000 1; //模拟生产一个产品printf(-Produce ---%d\n, mp-num);pthread_mutex_lock(lock);mp-next head;head mp;pthread_mutex_unlock(lock);pthread_cond_signal(has_product); //将等待在该条件变量上的一个线程唤醒sleep(rand() % 5);} } int main(int argc, char *argv[]) {pthread_t pid, cid;srand(time(NULL));pthread_create(pid, NULL, producer, NULL);pthread_create(cid, NULL, consumer, NULL);pthread_join(pid, NULL);pthread_join(cid, NULL);return 0; } 运行结果: ubuntu1604ubuntu:~/wangqinghe/linux/20190820$ ./conditionVar_product_consumer -Produce ---276 -Consume ---276 -Produce ---514 -Consume ---514 -Produce ---889 -Consume ---889 ^C 条件变量的优点        相较于mutex而言条件变量可以减少竞争。 如直接使用mutex除了生产者、消费者之间要竞争互斥量以外消费者之间也需要竞争互斥量但如果汇聚链表中没有数据消费者之间竞争互斥锁是无意义的。有了条件变量机制以后只有生产者完成生产才会引起消费者之间的竞争。提高了程序效率。 /*** condition.c ***/ #includestdio.h #includeunistd.h #includepthread.h #includestdlib.hstruct msg {struct msg *next;int num; };struct msg *head; struct msg *mp;pthread_cond_t has_product PTHREAD_COND_INITIALIZER; pthread_mutex_t lock PTHREAD_MUTEX_INITIALIZER;void *consumer(void *p) {for(;;){pthread_mutex_lock(lock);while(head NULL){pthread_cond_wait(has_product,lock);}mp head;head mp-next;pthread_mutex_unlock(lock);printf(-Consumer ---%d\n,mp-num);free(mp);mp NULL;sleep(rand() % 5);} }void *producer(void *p) {for(;;){mp malloc(sizeof(struct msg));mp-num rand() %1000 1;printf(-Producer ---%d\n,mp-num);pthread_cond_signal(has_product);sleep(rand() % 5);} }int main() {pthread_t pid,cid;srand(time(NULL));pthread_create(pid,NULL,producer,NULL);pthread_create(cid,NULL,consumer,NULL);pthread_join(pid,NULL);pthread_join(cid,NULL);return 0; } 运行结果 ubuntu1604ubuntu:~/wangqinghe/linux/20190820$ ./condition -Producer ---431 -Producer ---160 -Producer ---719 -Producer ---240 -Producer ---304 -Producer ---408 -Producer ---946 -Producer ---619 -Producer ---412 -Producer ---997 -Producer ---489 -Producer ---295 ^C   转载于:https://www.cnblogs.com/wanghao-boke/p/11389840.html
http://www.zqtcl.cn/news/165962/

相关文章:

  • 免费备案网站空间爱营销app
  • 郑州网站建设公网站建设需要步骤
  • 源创派网站建设做软件赚钱的网站有哪些
  • 中英文网站建设公司推广引流
  • 网站改域名百度热词指数
  • 网站开发工程师工作内容网站源码是用什么做的
  • 做网站优化费用免费的视频网站如何赚钱
  • 如何制作一个好网站中国建设银行网站暑假工报名
  • 阿里巴巴做网站找谁网站建设需要ui吗
  • 如何评价伊利集团网站建设长沙专业竞价优化首选
  • 网站建设费用标准做网站怎么盈利
  • 仕德伟做的网站图片怎么修initial wordpress
  • 网站制作公司多少费用正规的机械外包加工订单网
  • 网站的维护和推广2345网址大全设主页访问
  • 天津商城网站建设公司如何申请注册企业邮箱
  • 做家旅游的视频网站好给我一个可以在线观看的免费
  • 香奈儿网站建设做网站应该问客户什么需求
  • 永久免费ppt下载网站互联网上市公司一览表
  • 甘肃省建设工程168网站东营智能网站设计
  • 网站跨机房建设方案山西运城市建设局网站
  • 网站被k文章修改设计师图片素材
  • 建设银行益阳市分行桃江支行网站9377烈焰传奇手游官网
  • 网站收费怎么做沈阳建设工程信息网 等级中项网
  • 做网站后台教程视频杭州网站开发建设
  • 维度 网站建设优秀vi设计网站
  • 快速搭建网站工具海洋网络做网站不负责
  • 做电影资源网站服务器怎么选wordpress唱片公司模板
  • 医院网站建设投标要求wordpress文章的表是什么
  • 怎么做网站后门海外营销推广
  • 网站建设中英版网站要做手机版怎么做的