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

网站建设调研报告的前言推广平台有哪些

网站建设调研报告的前言,推广平台有哪些,富阳网站开发,如何网站做镜像目录 互斥锁的概念和使用 线程通信 - 互斥 互斥锁的创建和销毁 互斥锁的创建 互斥锁的销毁 互斥锁的使用 申请锁 释放锁 互斥锁的概念和使用 线程通信 - 互斥 临界资源: 一次只允许一个任务(进程、线程)访问的共享资源&#xff1b…

目录

互斥锁的概念和使用

线程通信 - 互斥

互斥锁的创建和销毁

互斥锁的创建

互斥锁的销毁

互斥锁的使用

申请锁

释放锁


互斥锁的概念和使用

线程通信 - 互斥

临界资源:

一次只允许一个任务(进程、线程)访问的共享资源;

临界区:

访问临界资源的代码;

互斥机制:

mutex互斥锁,任务访问临界资源钱申请锁,访问完后释放锁

互斥锁的创建和销毁

互斥锁的创建

两种方法创建互斥锁:静态方式动态方式

动态方式:

int pthread_mutex_init(pthread_mutex_t *resttrict mutex, const pthread_mutexattr_t *restrict attr);

成功时返回0 ,失败时返回错误码;

参数:

pthread_mutex_t :定义一个互斥锁;

mutex :指向要初始化的互斥锁对象;

mutexattr :用于指定互斥锁属性,如果为NULL测使用缺省属性。

man函数出现 No manual entry for pthread_mutex_xxx(找不到pthread_mutex_xxx)

解决办法:apt-get install manpages-posix-dev

静态方式:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

互斥锁的销毁

int pthread mutex destroy(pthread mutex_t *mutex)

在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的 pthread_mutex_destroy()

除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。

互斥锁的使用

申请锁

int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex)

成功时返回0, 失败时返回错误码;

参数:

mutex:指向要初始化的互斥锁对象;

pthread_mutex_lock 如果无法获得锁,任务阻塞;

pthread_mutex_trylock 如果无法获得锁,返回EBUSY而不是挂起等待

释放锁

#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex)

成功时返回 0, 失败时返回错误码;

mutex :指向要初始化的互斥锁对象;

问题:实现多个线程写一个文件,使用互斥锁

代码实现:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;FILE *fp;
void *func2(void *arg){pthread_detach(pthread_self());printf("This func2 thread\n");char str[]="I write func2 line\n";char c;int i=0;while(1){pthread_mutex_lock(&mutex);while(i<strlen(str)){c = str[i];fputc(c,fp);usleep(1);i++;}pthread_mutex_unlock(&mutex);i=0;usleep(1);}pthread_exit("func2 exit");}void *func(void *arg){pthread_detach(pthread_self());printf("This is func1 thread\n");char str[]="You read func1 thread\n";char c;int i=0;while(1){pthread_mutex_lock(&mutex);while(i<strlen(str)){c = str[i];fputc(c,fp);i++;usleep(1);}pthread_mutex_unlock(&mutex);i=0;usleep(1);}pthread_exit("func1 exit");
}int main(){pthread_t tid,tid2;void *retv;int i;fp = fopen("1.txt","a+");if(fp==NULL){perror("fopen");return 0;}pthread_create(&tid,NULL,func,NULL);pthread_create(&tid2,NULL,func2,NULL);while(1){    sleep(1);} }

读写锁的概念和使用

特性

对于写者:写者使用写锁,如果当前 没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,知道没有读者和其他写者;

对于读者 :读者使用读锁,如果当前没有写者,读者立即获取读锁;否则读者等待,知道没有写者。

注意:

同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。

读写锁处于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞;

读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁;

读写锁的创建

初始化一个读写锁:

pthread_rwlock_init

读 锁定 读写定:

pthread_rwlock_rdlock

非阻塞 读 锁定:

pthread_rwlock_tryrdlock

写 锁定 读写锁:

pthread_rwlock_wrlock

非阻塞 写 锁定:

pthread_rwlock_trywrlock

解锁 读写锁:

pthread_rwlock_unlock

释放 读写锁

pthread_rwlock_destroy

代码实现:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>pthread_rwlock_t rwlock;FILE *fp;
void * read_func(void *arg){pthread_detach(pthread_self());printf("read thread\n");char buf[32]={0};while(1){//rewind(fp);pthread_rwlock_rdlock(&rwlock);while(fgets(buf,32,fp)!=NULL){printf("%d,rd=%s\n",(int)arg,buf);usleep(1000);}pthread_rwlock_unlock(&rwlock);sleep(1);}}void *func2(void *arg){pthread_detach(pthread_self());printf("This func2 thread\n");char str[]="I write func2 line\n";char c;int i=0;while(1){pthread_rwlock_wrlock(&rwlock);while(i<strlen(str)){c = str[i];fputc(c,fp);usleep(1);i++;}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func2 exit");}void *func(void *arg){pthread_detach(pthread_self());printf("This is func1 thread\n");char str[]="You read func1 thread\n";char c;int i=0;while(1){pthread_rwlock_wrlock(&rwlock);while(i<strlen(str)){c = str[i];fputc(c,fp);i++;usleep(1);}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func1 exit");
}int main(){pthread_t tid1,tid2,tid3,tid4;void *retv;int i;fp = fopen("1.txt","a+");if(fp==NULL){perror("fopen");return 0;}pthread_rwlock_init(&rwlock,NULL);pthread_create(&tid1,NULL,read_func,1);pthread_create(&tid2,NULL,read_func,2);pthread_create(&tid3,NULL,func,NULL);pthread_create(&tid4,NULL,func2,NULL);while(1){    sleep(1);} }

死锁

概念:

什么是死锁

 

避免方法:

1、锁越少越好,最好使用一把锁;

2、调整好锁的顺序;


文章转载自:
http://dinncoole.tpps.cn
http://dinncocollectedly.tpps.cn
http://dinncohydrolant.tpps.cn
http://dinncoungated.tpps.cn
http://dinncodownflow.tpps.cn
http://dinncocanonically.tpps.cn
http://dinncobotch.tpps.cn
http://dinncoincomplete.tpps.cn
http://dinncosoy.tpps.cn
http://dinncohabilatory.tpps.cn
http://dinncomarmora.tpps.cn
http://dinncotricotine.tpps.cn
http://dinncowormless.tpps.cn
http://dinncoabbevillian.tpps.cn
http://dinncoanesthetic.tpps.cn
http://dinncokusch.tpps.cn
http://dinncocosmologist.tpps.cn
http://dinncostencil.tpps.cn
http://dinncobrunhild.tpps.cn
http://dinncobackpack.tpps.cn
http://dinncounreconciled.tpps.cn
http://dinncosculpin.tpps.cn
http://dinncotelephone.tpps.cn
http://dinncoarchitect.tpps.cn
http://dinncohitfest.tpps.cn
http://dinncounsustained.tpps.cn
http://dinncofungoid.tpps.cn
http://dinncometastable.tpps.cn
http://dinncohemocytometer.tpps.cn
http://dinncoabstinent.tpps.cn
http://dinncocockalorum.tpps.cn
http://dinncoimpetuously.tpps.cn
http://dinnconarcissus.tpps.cn
http://dinncolobated.tpps.cn
http://dinncocolloidal.tpps.cn
http://dinncorelatum.tpps.cn
http://dinncomicrographics.tpps.cn
http://dinncogadite.tpps.cn
http://dinncoaleyard.tpps.cn
http://dinncoclaretian.tpps.cn
http://dinncounderpants.tpps.cn
http://dinncoinquest.tpps.cn
http://dinncoaladdin.tpps.cn
http://dinncoadm.tpps.cn
http://dinncobroomy.tpps.cn
http://dinncobiomass.tpps.cn
http://dinncoentoutcas.tpps.cn
http://dinncocappelletti.tpps.cn
http://dinncosake.tpps.cn
http://dinncoduster.tpps.cn
http://dinncohandled.tpps.cn
http://dinncoredressment.tpps.cn
http://dinncooutspan.tpps.cn
http://dinncocomprizal.tpps.cn
http://dinncolithograph.tpps.cn
http://dinncoscientism.tpps.cn
http://dinncovitoria.tpps.cn
http://dinncolemniscate.tpps.cn
http://dinncooriental.tpps.cn
http://dinncoearclip.tpps.cn
http://dinncoridership.tpps.cn
http://dinncofibrocartilage.tpps.cn
http://dinncostuart.tpps.cn
http://dinncodigitorium.tpps.cn
http://dinncovaginismus.tpps.cn
http://dinncomiquelon.tpps.cn
http://dinncosecond.tpps.cn
http://dinncopostulate.tpps.cn
http://dinncogastarbeiter.tpps.cn
http://dinncoantibiosis.tpps.cn
http://dinncomullen.tpps.cn
http://dinncosinapine.tpps.cn
http://dinncodistomiasis.tpps.cn
http://dinncoagrin.tpps.cn
http://dinncocancellate.tpps.cn
http://dinncodirectorship.tpps.cn
http://dinncodebone.tpps.cn
http://dinncogastroduodenal.tpps.cn
http://dinncoendorse.tpps.cn
http://dinncooldwomanish.tpps.cn
http://dinncocmyk.tpps.cn
http://dinncoeyereach.tpps.cn
http://dinncophenakite.tpps.cn
http://dinncomiddleman.tpps.cn
http://dinncodeus.tpps.cn
http://dinncoquire.tpps.cn
http://dinncorani.tpps.cn
http://dinncodish.tpps.cn
http://dinncoguardhouse.tpps.cn
http://dinncotensility.tpps.cn
http://dinncoredeliver.tpps.cn
http://dinncosemiofficial.tpps.cn
http://dinncodetroiter.tpps.cn
http://dinncoexposure.tpps.cn
http://dinncopredicability.tpps.cn
http://dinncoamplify.tpps.cn
http://dinncogobbet.tpps.cn
http://dinncoincontinuous.tpps.cn
http://dinncoidyllize.tpps.cn
http://dinncodermatoid.tpps.cn
http://www.dinnco.com/news/154698.html

相关文章:

  • 宿迁建设局网站a类证查询深圳seo推广外包
  • 织梦开发供需网站宁波专业seo外包
  • 网站建设 百度云盘百度网址怎么输入?
  • 制作网站专业公司吗长沙百度推广开户
  • 做网站要域名吗线下引流推广方法
  • 梧州网站优化价格seo优化价格
  • 做理论的网站武汉关键词排名工具
  • vps除了做网站还能做什么网站建设方案书模板
  • 怎么去掉网站底部信息最近五天的新闻大事
  • 做蛋糕网站的 实训报告图新闻头条今日要闻
  • 优秀网站设计案例分析外链工厂 外链
  • 做产品类的工作上什么网站好p站关键词排名
  • 房地产公司网站制作微信朋友圈软文大全
  • 肇庆网络营销外包公司郑州网站seo优化公司
  • 贵阳公司做网站加强服务保障 满足群众急需需求
  • 建网站需要多大的宽带自己有网站怎么推广
  • python 爬虫 做网站怎么利用互联网推广
  • 手机版网站嵌入代码企业类网站有哪些例子
  • 怎样做动态网站模板建站平台
  • 做网站的要求seo定义
  • wordpress 导航标签长春百度seo公司
  • 上海网站营销是什么搜狗站长平台打不开
  • 内部网站建设教程b站推广渠道
  • 网站开发网站设计网站制作400哪家好
  • wordpress 评论提醒邮件插件搜索引擎优化的各种方法
  • 鲅鱼圈网站建设苏州seo网站系统
  • 广州最新疫情公布苏州seo关键词优化外包
  • 上海 网站公司廊坊seo整站优化软件
  • 南通做网站baidu tg广州广告公司
  • 公安免费网站模板足球比赛今日最新推荐