线程安全你不会,你面试,你怎么敢的呀,细到恐怖.......
2021-04-19 08:55
931 查看
线程安全
概念:多个线程对临界资源的访问是安全的;
实现:同步与互斥
- 互斥:通过同一时间对临界资源的唯一访问保证访问操作的安全;
- 同步:通过条件判断使对临界资源的访问更加合理;
互斥的实现:
互斥锁:本质是一个个0/1技术器,用于标记对临界资源的访问;
0——不可访问,1——可访问;
互斥锁自身操作是一个原子操作(直接用0与临界资源进行交换,然后判断临界资源可否访问):
//linux,打印会出错 //多线程打印,判断不及时,多线程进入导致--过多,打印错误 #include<stdio.h> 2 #include<pthread.h> 3 4 int tickets=100; 5 void *scalpers(void *arg){ 6 while(1){ 7 if(tickets>0){ 8 usleep(1); 9 printf("i get a ticket:%d\n",tickets); 10 tickets--; 11 } 12 else{ 13 pthread_exit(NULL); 14 } 15 } 16 return NULL; 17 } 18 int main(int argc,int *argv[]) 19 { 20 pthread_t tid[4]; 21 int ret; 22 int i=0; 23 for(i=0;i<4;++i){ 24 ret=pthread_create(&tid[i],NULL,scalpers,NULL); 25 if(ret!=0){ 26 printf("create error\n"); 27 return -1; 28 } 29 } 30 int j=0; 31 for(j=0;j<4;++j){ 32 pthread_join(tid[j],NULL); 33 34 } 35 return 0; 36 }
接口介绍
1.定义互斥锁变量:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
2. 初始化互斥锁(线程创建之前):
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); //mutex:要初始化的互斥量; // attr:NULL
3.在临界资源访问之前加锁:
int pthread_mutex_lock(pthread_mutex_t *mutex); //——阻塞接口
int pthread_mutex_trylock(pthread_mutex_t *mutex); //——非阻塞接口
4.在临界资源访问完毕后解锁:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
5.销毁互斥锁:
int pthread_mutex_destroy(pthread_mutex_t *mutex);
代码如下
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 #include <pthread.h> 5 6 int tickets = 100; 7 8 void *scalpers(void *arg) 9 { 10 pthread_mutex_t *mutex = (pthread_mutex_t*)arg; 11 while(1) { 12 pthread_mutex_lock(mutex);//3.加锁 13 if (tickets > 0) { 14 usleep(1); 15 printf("I got a ticket:%d\n", tickets); 16 tickets--; 17 }else { 18 pthread_mutex_unlock(mutex);//4.解锁 19 pthread_exit(NULL); 20 } 21 pthread_mutex_unlock(mutex);//4.解锁 22 } 23 return NULL; 24 } 25 int main (int argc, char *argv[]) 26 { 27 pthread_mutex_t mutex;//1.定义互斥锁变量 28 pthread_t tid[4]; 29 int ret; 30 pthread_mutex_init(&mutex, NULL);//2.初始化互斥锁 31 int i=0; 32 for(i = 0; i < 4; i++) { 33 ret = pthread_create(&tid[i], NULL, scalpers, &mutex); 34 if (ret != 0) { 35 printf("thread create error\n"); 36 return -1; 37 } 38 } 39 int j=0; 40 for (j = 0; j < 4; j++) { 41 pthread_join(tid[j], NULL); 42 } 43 pthread_mutex_destroy(&mutex);//5.销毁互斥锁 44 return 0; 45 }
线程的知识点:
https://blog.csdn.net/weixin_52270223/article/details/115820547
-
下个一博客介绍死锁的知识点;
如有错误或者补充,评论下;互相学习,互关一波,抱拳了
相关文章推荐
- 面试经验分享:遇到不会回答的问题怎么回答才好
- 面试检查自己的水平-不会英语怎么行!
- 面试时遇到不会回答的问题怎么办??
- 面试-工作中遇到不会的技术难题怎么办?
- Java开发岗位面试题归类---怎么好好的准备面试,也算是发展学习方向
- [转]知乎上的一个怎么面试iOS工程师的问题
- 不会编程该怎么成立初创公司呢?
- 微信怎么打开链接才不会提示已停止访问该网页,微信跳转浏览器
- 不会有人还不知道全文检索工具Lucene怎么用吧?文字长文教程
- android 开发怎么让程序生成的图片文件不会被系统扫描到
- 微信浏览器怎么打开链接才不会提示已停止访问该网页
- 怎么拿下Java工程师职位 有哪些面试小技巧
- 面试被问:Redis 内存满了怎么办?
- 【面试】如果你这样回答“什么是线程安全”,面试官都会对你刮目相看
- 外挂是怎么用的啊,我不会啊,请朋友帮忙啊
- 某网贴出来的u3d面试题目汇总,当时学习下(好多我都不会呢)
- 面试 | 创建多少个线程合适,该怎么回答?
- 不会装cuda配环境的小学生怎么躺撸caffe
- 怎么理解StringBuffer线程安全,StringBuilder线程不安全
- 连跳槽都不会,还怎么做社会主义接班人!