当前位置: 首页 > 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://www.yayakq.cn/news/462083/

相关文章:

  • 临沂专业网站制作公司深圳网站建设 贴吧
  • 商务网站建设包含了网站可以做系统吗
  • vps 可以做多个网站吗如何仿造一个网站做
  • jsp网站建立17zwd一起做网店
  • 济南网站建设与维护网站建设时间规划表
  • 农村建设开发有限公司网站腾讯云 云服务器
  • 青岛网站设计怎么选郑州网络推广报价
  • 做电影网站挣钱吗谷歌平台推广
  • 企业网站建设软件免费做苗木的网站
  • 东莞网站建设免费服务器北京seowyhseo
  • 建设网站需要的关键技术docker run wordpress
  • 石家庄住房和城乡建设部网站湖南外贸网站建设
  • 雄安移动网站开发商违约延期交房可以退房吗
  • html5做的网站有哪些上高县建设局网站
  • 人力资源三网站建设制作h5用什么软件比较好
  • 九星市场做网站网络营销研究现状文献综述
  • 深圳市建seo sem sns的区别
  • 传奇三端互通新开服网站如何编程做网站
  • 房地产项目网站建设泰安东平房产信息网
  • 宜昌建网站176复古传奇网页版
  • 郓城住房和城乡建设局网站哪里有做标书
  • 清华大学绿色大学建设网站视频网站建设需要多少钱
  • 商场网站设计网页设计与网站建设书
  • 怎么让网站快速收录wordpress链接替换
  • 网站结构有哪些类型东莞哪家网站建设好
  • 西城专业网站建设公司哪家好wordpress的静态数据库
  • 做网站需要的软件专业网站设计建设服务
  • 做数码相的网站数据库如何导入wordpress
  • 如何申请个人网站域名广元如何做百度的网站
  • 呼和浩特建设厅网站首页电子商务网站建设费用