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

无锡上网站建设Wordpress和drupal开发

无锡上网站建设,Wordpress和drupal开发,什么网站备案比较快,旅游管理网站业务模块条件变量 条件变量本身不是锁#xff0c;但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。 使用互斥量保护共享数据使用条件变量可以造成线程阻塞#xff0c;等待某个条件的发生#xff0c;当条件满足的时候解除阻塞。 条件变量的两个动作但是它可以造成线程阻塞。通常于互斥锁配合使用。给多线程提供一个会和的场合。 使用互斥量保护共享数据使用条件变量可以造成线程阻塞等待某个条件的发生当条件满足的时候解除阻塞。 条件变量的两个动作 条件不满足阻塞线程条件满足通知阻塞的线程解除阻塞 相关函数 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失败返回错误号 生产者与消费者模型 #includestdio.h #includestring.h #includestdlib.h #includesys/types.h #includeunistd.h #includepthread.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 *pNodeNULL;pNode(node*)malloc(sizeof(node));if(pNodeNULL){printf(malloc error);exit(-1);} //加锁pthread_mutex_lock(mutex);pNode-datarand()%100;//随机生成数printf(p:[%d]\n,pNode-data); //头插法pNode-nextphead;pheadpNode; //解锁pthread_mutex_unlock(mutex); //唤醒至少一个线程pthread_cond_signal(cond);sleep(1);//防止生成过快导致内存不足} } void *consumer(void *arg) {while(1){pthread_mutex_lock(mutex);//加锁if(pheadNULL){ //如果头节点为空那么阻塞并解锁 //如果头节点不为空接收到pthread_cond_signal的唤醒解除阻塞并加锁pthread_cond_wait(cond,mutex);}node*pNodephead;printf(c:[%d]\n,pNode-data); //头节点移动pheadphead-next; //释放当前节点free(pNode);pNodeNULL; //解锁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 retpthread_create(thread1,NULL,producer,NULL);if(ret!0){printf(pthread_create1 error:[%s]\n,strerror(ret));return -1;}retpthread_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掉。 #includestdio.h #includestring.h #includestdlib.h #includesys/types.h #includeunistd.h #includepthread.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 *pNodeNULL;pNode(node*)malloc(sizeof(node));if(pNodeNULL){printf(malloc error);exit(-1);}pthread_mutex_lock(mutex);pNode-datarand()%100;printf(p:[%d]\n,pNode-data);pNode-nextphead;pheadpNode;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(pheadNULL){pthread_cond_wait(cond,mutex);}if(pheadNULL){pthread_mutex_unlock(mutex);//先解锁因为pthread_cond_wait会加一个锁continue;}node*pNodephead;printf(c[%d]:[%d]\n,n,pNode-data);pheadphead-next;free(pNode);pNodeNULL;pthread_mutex_unlock(mutex);sleep(1);} } int main() {int i0;int arr[5];pthread_mutex_init(mutex,NULL);pthread_cond_init(cond,NULL);pthread_t thread1;pthread_t thread2;int retpthread_create(thread1,NULL,producer,NULL);if(ret!0){printf(pthread_create1 error:[%s]\n,strerror(ret));return -1;}for(;i5;i)//创建5个消费者子线程{arr[i]i;retpthread_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://www.pierceye.com/news/426864/

相关文章:

  • 做网站产品资料表格做鞋设备网站
  • 青海西宁制作网站企业做网站用什么语言编写
  • 网站变成手机网站做简历的网站都有哪些内容
  • 汽车网站建设页面软件公司都是帮别人做网站么
  • 微信的企业网站模板深圳做关键词优化平台
  • 用什么框架做网站快网站建设与维护薪资
  • 深圳公明做网站用织梦做网站都需要用什么
  • 广西城乡建设部网站首页国家住房和城乡建设部中国建造师网站
  • 杭州高端网站设计公司天津做网站那家好
  • 怎么建立网站?长沙做网站价格
  • 新网网站后台登陆程序员为什么35岁就不能干?
  • 百度申诉网站seo项目经理
  • 北京网站排名优化软件花箱 东莞网站建设
  • wordpress 迁站如何来建设网站
  • 营销型企业网站建设哪家好自己个人网站后台怎么做
  • 如何做网站内链优化网店运营的工作内容
  • 邢台网站设计cute wordpress主题破解版
  • 建站网站案例什么在线做动图的网站比较好
  • 云南做网站哪家便宜对象存储链接WordPress
  • 网站上传模板后ui设计界面配色
  • 阿里网站备案公众号小程序制作平台
  • 东莞网站建设seo公司为什么建立网站
  • 一个网站绑定多个域名可以做logo设计单子的网站
  • 哈尔滨市建设厅网站去国外做非法网站吗
  • 淮安网站建设要多少钱营销推广网歹
  • 洛阳建设企业网站成品app直播源码推荐
  • 网站值不值得做seo什么事三合一网站
  • 微网站开发协议中国建设部网站监理延续
  • 安阳网站建设公司wordpress评论模块
  • 做服装微商城网站wordpress后台载入慢