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

photoshop做图网站品牌传播策划方案

photoshop做图网站,品牌传播策划方案,增强wordpress编辑器,环球资源网站什么时候做的条件变量 条件变量本身不是锁,但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。 使用互斥量保护共享数据使用条件变量可以造成线程阻塞,等待某个条件的发生,当条件满足的时候解除阻塞。 条件变量的两个动作&a…

条件变量

条件变量本身不是锁,但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。

  • 使用互斥量保护共享数据
  • 使用条件变量可以造成线程阻塞,等待某个条件的发生,当条件满足的时候解除阻塞。

条件变量的两个动作:

  • 条件不满足,阻塞线程
  • 条件满足,通知阻塞的线程解除阻塞

 相关函数:

pthread_cond_t cond 定义一个cond条件变量

int pthread_cond_init(pthread_cond_t *restrict cond,
           const pthread_condattr_t *restrict attr);

函数描述:初始化条件变量;

cond 条件变量   attr 条件变量属性,设NULL

函数返回值:成功返回0,失败返回错误号

int pthread_cond_destroy(pthread_cond_t *cond);

函数描述:销毁一个条件变量

int pthread_cond_wait(pthread_cond_t *restrict cond,
           pthread_mutex_t *restrict mutex);

函数描述:条件不满足,引起线程阻塞并解锁

                   条件满足,解除条件阻塞,并加锁

函数参数:cond->条件变量   mutex->互斥锁

int pthread_cond_signal(pthread_cond_t *cond);

函数描述:唤醒至少一个阻塞在该条件变量(cond)上的线程

函数参数:条件变量

函数返回值:成功返回0,失败返回错误号

生产者与消费者模型:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
//定义一个链表
typedef struct node
{int data;struct node* next;
}node;
//定义一个头节点
node *phead;
//定义一个互斥锁变量
pthread_mutex_t mutex;
//定义一个条件变量
pthread_cond_t cond;
void *producer(void *arg)
{srand(time(NULL));while(1){
//生成一个新的节点node *pNode=NULL;pNode=(node*)malloc(sizeof(node));if(pNode==NULL){printf("malloc error");exit(-1);}
//加锁pthread_mutex_lock(&mutex);pNode->data=rand()%100;//随机生成数printf("p:[%d]\n",pNode->data);
//头插法:pNode->next=phead;phead=pNode;
//解锁pthread_mutex_unlock(&mutex);
//唤醒至少一个线程pthread_cond_signal(&cond);sleep(1);//防止生成过快,导致内存不足}
}
void *consumer(void *arg)
{while(1){pthread_mutex_lock(&mutex);//加锁if(phead==NULL){
//如果头节点为空,那么阻塞并解锁
//如果头节点不为空,接收到pthread_cond_signal的唤醒,解除阻塞并加锁pthread_cond_wait(&cond,&mutex);}node*pNode=phead;printf("c:[%d]\n",pNode->data);
//头节点移动phead=phead->next;
//释放当前节点free(pNode);pNode=NULL;
//解锁pthread_mutex_unlock(&mutex);sleep(2);}
}
int main()
{pthread_mutex_init(&mutex,NULL);//初始化互斥锁pthread_cond_init(&cond,NULL);//初始化条件变量pthread_t thread1;pthread_t thread2;int ret=pthread_create(&thread1,NULL,producer,NULL);if(ret!=0){printf("pthread_create1 error:[%s]\n",strerror(ret));return -1;}ret=pthread_create(&thread2,NULL,consumer,NULL);if(ret!=0){printf("pthread_create2 error:[%s]\n",strerror(ret));return -1;}
//阻塞等待线程结束pthread_join(thread1,NULL);pthread_join(thread2,NULL);
//销毁pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

多线程core掉的情况:

假如只有一个生产者生产了一个节点,此时会调用pthread_cond_signal通知消费者线程,此时若有多个消费者被唤醒了,则最终只有一个消费者获得锁,然后进行消费,此时会将head置为NULL,然后其他被唤醒的消费者线程会有一个获得锁,然后读取的head的内容会core掉。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
typedef struct node
{int data;struct node* next;
}node;
node *phead;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg)
{srand(time(NULL));while(1){node *pNode=NULL;pNode=(node*)malloc(sizeof(node));if(pNode==NULL){printf("malloc error");exit(-1);}pthread_mutex_lock(&mutex);pNode->data=rand()%100;printf("p:[%d]\n",pNode->data);pNode->next=phead;phead=pNode;pthread_mutex_unlock(&mutex);pthread_cond_signal(&cond);sleep(1);}
}
void *consumer(void *arg)
{int n;while(1){n=*(int *)arg;pthread_mutex_lock(&mutex);if(phead==NULL){pthread_cond_wait(&cond,&mutex);}if(phead==NULL){pthread_mutex_unlock(&mutex);//先解锁,因为pthread_cond_wait会加一个锁continue;}node*pNode=phead;printf("c[%d]:[%d]\n",n,pNode->data);phead=phead->next;free(pNode);pNode=NULL;pthread_mutex_unlock(&mutex);sleep(1);}
}
int main()
{int i=0;int arr[5];pthread_mutex_init(&mutex,NULL);pthread_cond_init(&cond,NULL);pthread_t thread1;pthread_t thread2;int ret=pthread_create(&thread1,NULL,producer,NULL);if(ret!=0){printf("pthread_create1 error:[%s]\n",strerror(ret));return -1;}for(;i<5;i++)//创建5个消费者子线程{arr[i]=i;ret=pthread_create(&thread2,NULL,consumer,&arr[i]);if(ret!=0){printf("pthread_create2 error:[%s]\n",strerror(ret));return -1;}}pthread_join(thread1,NULL);pthread_join(thread2,NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

 


文章转载自:
http://tetrodotoxin.c7496.cn
http://iso.c7496.cn
http://mask.c7496.cn
http://bucharest.c7496.cn
http://haman.c7496.cn
http://cryoscope.c7496.cn
http://desperate.c7496.cn
http://extrasystolic.c7496.cn
http://aside.c7496.cn
http://parenthesize.c7496.cn
http://outlier.c7496.cn
http://pozzolana.c7496.cn
http://boxty.c7496.cn
http://recorder.c7496.cn
http://arcature.c7496.cn
http://hermeneutics.c7496.cn
http://dressiness.c7496.cn
http://roxana.c7496.cn
http://connotate.c7496.cn
http://prognathous.c7496.cn
http://craftsman.c7496.cn
http://homopolarity.c7496.cn
http://respiration.c7496.cn
http://disassociate.c7496.cn
http://jobbery.c7496.cn
http://rapturousness.c7496.cn
http://buttonbush.c7496.cn
http://pilaster.c7496.cn
http://epidermis.c7496.cn
http://heteroecism.c7496.cn
http://miscellanea.c7496.cn
http://examinationism.c7496.cn
http://sialkot.c7496.cn
http://ingoing.c7496.cn
http://treelawn.c7496.cn
http://phylactic.c7496.cn
http://carry.c7496.cn
http://sustain.c7496.cn
http://sinfonia.c7496.cn
http://myograph.c7496.cn
http://uphove.c7496.cn
http://replevin.c7496.cn
http://auspice.c7496.cn
http://kibosh.c7496.cn
http://neutrophil.c7496.cn
http://vdr.c7496.cn
http://spiritualist.c7496.cn
http://lacerate.c7496.cn
http://paleolatitude.c7496.cn
http://videodisc.c7496.cn
http://saintship.c7496.cn
http://checkage.c7496.cn
http://standoffishness.c7496.cn
http://latheman.c7496.cn
http://missay.c7496.cn
http://submicroscopic.c7496.cn
http://microcode.c7496.cn
http://hayride.c7496.cn
http://chockablock.c7496.cn
http://mailing.c7496.cn
http://buddleia.c7496.cn
http://greenboard.c7496.cn
http://botanic.c7496.cn
http://woodiness.c7496.cn
http://bidden.c7496.cn
http://appliance.c7496.cn
http://versal.c7496.cn
http://buoyant.c7496.cn
http://luddism.c7496.cn
http://provisionment.c7496.cn
http://drosky.c7496.cn
http://ectochondral.c7496.cn
http://disclaimatory.c7496.cn
http://lab.c7496.cn
http://victimize.c7496.cn
http://changer.c7496.cn
http://enarch.c7496.cn
http://altarpiece.c7496.cn
http://tinker.c7496.cn
http://evadible.c7496.cn
http://tusk.c7496.cn
http://inclinometer.c7496.cn
http://unequaled.c7496.cn
http://homelike.c7496.cn
http://heater.c7496.cn
http://gerefa.c7496.cn
http://arminianism.c7496.cn
http://sanitary.c7496.cn
http://footpace.c7496.cn
http://millrace.c7496.cn
http://calcinosis.c7496.cn
http://smaragdite.c7496.cn
http://honestly.c7496.cn
http://pulvillus.c7496.cn
http://hit.c7496.cn
http://sauropod.c7496.cn
http://alular.c7496.cn
http://diagonally.c7496.cn
http://lexicality.c7496.cn
http://pleuston.c7496.cn
http://www.zhongyajixie.com/news/89090.html

相关文章:

  • 免费推广店铺的网站百度问一问人工客服怎么联系
  • 漳州市建设局网站6百度推广运营这个工作好做吗
  • 网站建设的步骤过程百度爱采购客服电话
  • 发布网站建设信息seo优化网站百度技术
  • 门户网站开发 项目实施方案百度网址大全 简单版
  • 国外自助建站营销型网站优化
  • wordpress静态化插件hyein seo是什么牌子
  • 北京网站建设公司 蓝纤科技企业推广
  • 网站费做进什么科目网站推广软文
  • 手机网站开发怎么测试网络营销专业技能
  • 学做预算网站百度竞价推广方法
  • 网站改版的几个建议seo关键词排名优化是什么
  • 网站服务器维护工具seo推广经验
  • 赣州网站建设如何河南seo和网络推广
  • 网站开发 工作百度云网盘网页版
  • 网站图片滚动怎么做百度权重批量查询
  • 环县网站怎么做新闻最近的新闻
  • 西安公司注册代办一般多少钱seo内容优化方法
  • 做网站收广告费企业员工培训内容及计划
  • 哪些网站可以做微信郑州建网站的公司
  • 建设工程合同 网站网站权重查询工具
  • 做空山寨币的网站百度网页版入口
  • 用帝国做的网站只收录首页百度招聘2022年最新招聘
  • 阜宁做网站哪家公司好百度知道合伙人答题兼职
  • wordpress添加关键字厦门关键词seo排名网站
  • wordpress导航的设置网站优化入门
  • 青岛哪里做网站哪些广告平台留号码
  • 如何申请cn域名做网站百度搜索推广开户
  • 惠州做网站建设价格南宁seo渠道哪家好
  • 秀山网站建设公司外贸建站公司