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

中山里水网站建设软文广告案例分析

中山里水网站建设,软文广告案例分析,网站后台更新栏目后 网站,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://www.hkea.cn/news/377280/

相关文章:

  • 做外贸是用什么网站做新型网络营销方式
  • 心理咨询网站开发百度手机seo软件
  • 17网站一起做网批seo营销优化
  • 做赚钱网站程序员培训班要多少钱
  • 已经收录大规模修改收录页面对网站有影响吗什么软件可以推广自己的产品
  • 丁香园做科室网站厦门网络推广
  • 免费的企业网站制作提高网站权重的方法
  • 兰州网站制作怎么样网页在线生成
  • 自建网站网址雅虎搜索引擎首页
  • 注册科技有限公司可以做网站吗百度搜索排名机制
  • 武汉做网站好网站制作多少钱一个
  • 安阳网站建设怎么从网上找客户
  • 文章博客媒体网站模板怎样在百度上打广告
  • 做网站是不是要模板直接打开百度
  • 哪个网站做app推广服务商
  • 中国哪里在大建设网站优化培训学校
  • 自己做的网站点首页出错腾讯广告代理商加盟
  • 如何做免费的网站推广东莞百度seo
  • 宜昌网站制作公司百度竞价官网
  • 建站公司网站模板论坛怎么建网站
  • 上海做b2b网站公司深圳公司网络推广该怎么做
  • 自己做的网站怎么在百度可以查到网络小说网站三巨头
  • 怎么做网站客服弹窗站长之家seo工具包
  • 自己建一个电商网站吗网络营销的定义
  • 专门做金融的招聘网站四川seo选哪家
  • wordpress nginx伪静态配置拼多多seo怎么优化
  • 深圳网站开发电话惠州网络营销
  • 中宁网站建设公司商城全网推广运营公司
  • 网站文章列表如何排版郑州seo技术培训班
  • 小型b2c网站百度开户渠道商哪里找