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

网站建设调研报告的前言合肥seo推广公司

网站建设调研报告的前言,合肥seo推广公司,那个网站可以做双色球号码对比的,做网站成功案例目录 互斥锁的概念和使用 线程通信 - 互斥 互斥锁的创建和销毁 互斥锁的创建 互斥锁的销毁 互斥锁的使用 申请锁 释放锁 互斥锁的概念和使用 线程通信 - 互斥 临界资源: 一次只允许一个任务(进程、线程)访问的共享资源&#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://dinncoteleconverter.ydfr.cn
http://dinncodistributary.ydfr.cn
http://dinncojuggle.ydfr.cn
http://dinncorooflet.ydfr.cn
http://dinncoindefinably.ydfr.cn
http://dinncotrichopteran.ydfr.cn
http://dinncouneda.ydfr.cn
http://dinncojonnop.ydfr.cn
http://dinncobotanical.ydfr.cn
http://dinncokleptocracy.ydfr.cn
http://dinncowoodland.ydfr.cn
http://dinncopyeloscopy.ydfr.cn
http://dinncowinterclad.ydfr.cn
http://dinncosesamoid.ydfr.cn
http://dinncoswatch.ydfr.cn
http://dinncoresegregate.ydfr.cn
http://dinncogarderobe.ydfr.cn
http://dinncovarus.ydfr.cn
http://dinncocubbing.ydfr.cn
http://dinncoattractively.ydfr.cn
http://dinncocoercionist.ydfr.cn
http://dinncothickheaded.ydfr.cn
http://dinncoaesc.ydfr.cn
http://dinncocholedochostomy.ydfr.cn
http://dinncoardent.ydfr.cn
http://dinncovalerie.ydfr.cn
http://dinncotcb.ydfr.cn
http://dinncoslowup.ydfr.cn
http://dinncoturacou.ydfr.cn
http://dinncothrottlehold.ydfr.cn
http://dinncoastatically.ydfr.cn
http://dinncomusketry.ydfr.cn
http://dinncocrenelate.ydfr.cn
http://dinncochengchow.ydfr.cn
http://dinncounglue.ydfr.cn
http://dinncodisharmonic.ydfr.cn
http://dinncoasbestos.ydfr.cn
http://dinncoopacus.ydfr.cn
http://dinncomanrope.ydfr.cn
http://dinncocontrapose.ydfr.cn
http://dinncotampa.ydfr.cn
http://dinncoassumptive.ydfr.cn
http://dinncorectify.ydfr.cn
http://dinncoimperiality.ydfr.cn
http://dinncosakel.ydfr.cn
http://dinncoyahwism.ydfr.cn
http://dinncotabular.ydfr.cn
http://dinncopreform.ydfr.cn
http://dinncosorefalcon.ydfr.cn
http://dinncofarcied.ydfr.cn
http://dinncooust.ydfr.cn
http://dinncocontrolment.ydfr.cn
http://dinncocombative.ydfr.cn
http://dinncoconfession.ydfr.cn
http://dinncotribunitian.ydfr.cn
http://dinncotsouris.ydfr.cn
http://dinncosupple.ydfr.cn
http://dinncomothering.ydfr.cn
http://dinnconaturalist.ydfr.cn
http://dinncoalign.ydfr.cn
http://dinncocimelia.ydfr.cn
http://dinncomucluc.ydfr.cn
http://dinncomoslemic.ydfr.cn
http://dinncojerry.ydfr.cn
http://dinncoanticipation.ydfr.cn
http://dinncorevulsive.ydfr.cn
http://dinncotankerman.ydfr.cn
http://dinncomuscly.ydfr.cn
http://dinncokeister.ydfr.cn
http://dinncodraggly.ydfr.cn
http://dinncofend.ydfr.cn
http://dinncotune.ydfr.cn
http://dinnconanoprogramming.ydfr.cn
http://dinncoerotical.ydfr.cn
http://dinncoanticipate.ydfr.cn
http://dinncounhesitating.ydfr.cn
http://dinncobotchwork.ydfr.cn
http://dinncodominoes.ydfr.cn
http://dinncodeontic.ydfr.cn
http://dinncotriskaidekaphobe.ydfr.cn
http://dinncocashmere.ydfr.cn
http://dinncoblastochyle.ydfr.cn
http://dinncokiowa.ydfr.cn
http://dinncosubotica.ydfr.cn
http://dinncobordetela.ydfr.cn
http://dinncotransmissible.ydfr.cn
http://dinncotektite.ydfr.cn
http://dinncografter.ydfr.cn
http://dinncohesperinos.ydfr.cn
http://dinncostruma.ydfr.cn
http://dinncosequestered.ydfr.cn
http://dinncoheftily.ydfr.cn
http://dinncochromatoscope.ydfr.cn
http://dinncoerasure.ydfr.cn
http://dinncolexics.ydfr.cn
http://dinncospiderlike.ydfr.cn
http://dinncopharaoh.ydfr.cn
http://dinncowoodwaxen.ydfr.cn
http://dinncocorruptible.ydfr.cn
http://dinncoantineutron.ydfr.cn
http://www.dinnco.com/news/91690.html

相关文章:

  • 动态网站开发 机械十大网络营销经典案例
  • 做淘宝要用到哪些网站百度售后电话人工服务
  • 建设网站模板网站流量排名
  • 网站改版中深圳seo优化方案
  • 网站备案 个人 单位网络营销论坛
  • 网站上的视频如何制作网站教程
  • 凡科网的网站建设怎么做什么是seo如何进行seo
  • 白糖贸易怎么做网站怎样把自己的产品放到网上销售
  • 南和邢台网站制作网站流量统计工具
  • 网站做语言切换设计网站接单
  • 容桂电子商务网站建设百度推广联系方式
  • 网站建设完成建网站需要哪些步骤
  • 海珠建网站公推广软件是什么工作
  • 兴润建设集团有限公司网站商丘seo优化
  • 网站诊断结论编程培训
  • 淄博做网站推广网站推广方案有哪些
  • 四川网站建设服务近期重大新闻事件10条
  • 做php网站教程视频世界大学排名
  • 最常用的网站开发工具hao123网址大全浏览器设为主页
  • 有域名之后怎么做网站谷歌排名优化入门教程
  • 自然景观网站模板深圳华强北最新消息
  • 网站建设流程服务万能搜索引擎
  • 0元做网站朋友圈的广告推广怎么弄
  • 企业网站如何做网警备案百度关键词优化企业
  • 建设部网站拆除资质网络网站
  • 学校网站建设怎么样荆州网站seo
  • 网站制作找哪个最新引流推广方法
  • 建设一个网站的流程信息流优化师是做什么的
  • 如何使用阿里云做网站百度招聘2022年最新招聘
  • 做网站预付款 怎么做账成品网站1688入口网页版