程序员

线程安全你不会,你面试,你怎么敢的呀,细到恐怖.......

作者:admin 2021-08-10 我要评论

线程安全 概念多个线程对临界资源的访问是安全的 实现同步与互斥 互斥通过同一时间对临界资源的唯一访问保证访问操作的安全 同步通过条件判断使对临界资源的访问...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

线程安全

概念:多个线程对临界资源的访问是安全的;

实现:同步与互斥

  • 互斥:通过同一时间对临界资源的唯一访问保证访问操作的安全;

  • 同步:通过条件判断使对临界资源的访问更加合理;

互斥的实现:

互斥锁:本质是一个个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

下个一博客介绍死锁的知识点;
如有错误或者补充,评论下;互相学习,互关一波,抱拳了

;原文链接:https://blog.csdn.net/weixin_52270223/article/details/115835481

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 十月更新修复了Windows 10的Ping of De

    十月更新修复了Windows 10的Ping of De

  • Windows10 UAC弹窗太烦但又不能关?教

    Windows10 UAC弹窗太烦但又不能关?教

  • 老大手把手教我玩 Git 变基!

    老大手把手教我玩 Git 变基!

  • 在Linux终端中展示幻灯片

    在Linux终端中展示幻灯片

腾讯云代理商
海外云服务器