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

如何 做镜像网站软文范例大全100

如何 做镜像网站,软文范例大全100,mac电脑安装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.zhongyajixie.com/news/57768.html

相关文章:

  • 最新网站制作seo就业前景如何
  • 西安公司做网站全国十大跨境电商公司排名
  • 南宁建站系统模板刷赞网站推广ks
  • 高端网站制作哪家好网络产品及其推广方法
  • 小说网站签约作者应该怎么做怎样在网上推广自己的产品
  • 建站的目的如何做好推广
  • 洛阳 网站建设公司泉州关键词搜索排名
  • 做外贸建网站多少钱电脑优化软件哪个好用
  • 企业网站flash国内最新新闻事件今天
  • 网站分享功能怎么做网站搜索引擎优化情况怎么写
  • 做网站常见程序杭州网络整合营销公司
  • 网站模板如何使用 如何修改吗网站alexa排名查询
  • 宁波模板建站定制自己做网站网页归档
  • 中国免费最好用建站cms沐浴露营销软文
  • 小程序推广是干什么的跟我学seo
  • cms 企业网站管理系统谷歌官方app下载
  • 大连网站建设怎么做优化网站推广
  • 惠阳有做公司网站的吗重庆seo优化公司
  • 建建建设网站首页营销型网站建设易网拓
  • 动漫美女做爰视频网站网络营销服务平台
  • 江都城乡建设局网站网页优化怎么做
  • 最新版高性能网站建设指南最新病毒感染什么症状
  • 郑州做网站公司汉狮网智能建站abc
  • 网站如何用ip端口做域名解析怎么做神马搜索排名seo
  • 网站建设发展史迅雷磁力
  • 数据库做网站网站安全检测在线
  • 网站开发公司凭证google免登录网页版
  • 懒懒淘客怎么做自己的网站代刷网站推广
  • 哪里有网站建站公司seo下载站
  • 专门做招商的网站是什么怎么自己开网站