线程的同步和互斥学习笔记

这篇具有很好参考价值的文章主要介绍了线程的同步和互斥学习笔记。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

互斥锁的概念和使用 

线程通信-互斥

 互斥锁的创建和销毁

 申请锁-pthread_mutex_lock

 释放锁-pthread_mutex_unlock

读写锁的概念和使用

死锁的避免


互斥锁的概念和使用 

线程通信-互斥

临界资源

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

概念:

        不能同时访问的资源,比如写文件,只能由一个线程写,同时写会写乱。

        比如外设打印机,打印的时候只能由一个程序使用。

        外设基本上都是不能共享的资源。

        生活中比如卫生间,同一时间只能由一个人使用。

临界区

  • 访问临界资源的代码

互斥机制

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

 互斥锁的创建和销毁

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

动态方式: 

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex,
    const pthread_mutexattr_t *attr);
  • 成功时返回0,失败时返回错误码
  • mutex指向要初始化的互斥锁对象
  • attr互斥锁属性,NULL表示缺省属性
  • man函数出现No manual entry for pthread_mutex_xxx解决方法:apt-get install manpages-posix-dev

 静态方式:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

 锁的销毁:

int pthread_mutex_destory(pthread_mutex_t *mutex)

在linux中,互斥锁并不占用任何资源,因此LinuxThreads中的pthread_mutex_destory()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。

 申请锁-pthread_mutex_lock

#include <pthread.h>
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而不是挂起等待

 释放锁-pthread_mutex_unlock

#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
  • 成功时返回0,失败时返回错误码
  • mutex指向要初始化的互斥锁对象

 示例代码:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  //多个文件需要多个锁
void *func1(void *arg)
{
    pthread_detach(pthread_self());
    printf("This is child thread1\n");
    char str[] = "I write func1 line\n";
    char c;
    int i = 0;
    //pthread_mutex_t mutex1;
    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("func1 exit");
}
void *func2(void *arg)
{
    pthread_detach(pthread_self());
    printf("This is child thread2\n");
    char str[] = "You read func1 thread\n";
    char c;
    int i = 0;
    //pthread_mutex_t mutex2;
    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");
}
int main()
{ 
    pthread_t tid1,tid2;
    void *retv;
    int i;   
    fp = fopen("1.txt","a+");
    if(fp == NULL)
    {
        perror("fopen");
        return 0;
    }
    pthread_create(&tid1,NULL,func1,NULL);
    pthread_create(&tid2,NULL,func2,NULL);
    while(1)
    {
        sleep(1);
    }
}

运行结果:

线程的同步和互斥学习笔记,Linux学习笔记,学习,笔记,linux

读写锁的概念和使用

 必要性:提高线程执行效率

特性:

  • 写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。
  • 读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。

 注意:

  • 同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁
  • 读写锁出于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。
  • 读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁
  •  初始化一个读写锁        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);
    } 

}

死锁的避免

  • 锁越少越好,最好使用一把锁
  • 调整好锁的顺序
  • 使锁进行错位

示例代码:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  //多个文件需要多个锁
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;  //多个文件需要多个锁

void *func1(void *arg)
{
    pthread_detach(pthread_self());
    printf("This is child thread1\n");
    char str[] = "I write func1 line\n";
    char c;
    int i = 0;
    //pthread_mutex_t mutex1;
    while (1)
    {
        pthread_mutex_lock(&mutex2);
        printf("%d,I get lock2\n",(int)arg);
        sleep(1);
        pthread_mutex_lock(&mutex);
        printf("%d,I get 2 locks\n",(int)arg);
        pthread_mutex_unlock(&mutex);
        pthread_mutex_unlock(&mutex2);
        sleep(10);
    }
    
    pthread_exit("func1 exit");
}
void *func2(void *arg)
{
    pthread_detach(pthread_self());
    printf("This is child thread2\n");
    char str[] = "You read func1 thread\n";
    char c;
    int i = 0;
    //pthread_mutex_t mutex2;
    while (1)
    {
        pthread_mutex_lock(&mutex);
        printf("%d,I get lock1\n",(int)arg);
        sleep(1);
        pthread_mutex_lock(&mutex2);
        printf("%d,I get 2 locks\n",(int)arg);

        pthread_mutex_unlock(&mutex2);
        pthread_mutex_unlock(&mutex);
        usleep(10);
    }
    pthread_exit("func2 exit");
}
int main()
{ 
    pthread_t tid1,tid2;
    void *retv;
    int i;   
    fp = fopen("1.txt","a+");
    if(fp == NULL)
    {
        perror("fopen");
        return 0;
    }
    pthread_create(&tid1,NULL,func1,1);
    sleep(5);
    pthread_create(&tid2,NULL,func2,2);
    while(1)
    {
        sleep(1);
    }
}

 运行结果:

线程的同步和互斥学习笔记,Linux学习笔记,学习,笔记,linux文章来源地址https://www.toymoban.com/news/detail-822424.html

到了这里,关于线程的同步和互斥学习笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux-线程的同步与互斥

    🚀 临界资源:多线程指行流共享的资源叫做临界资源。 🚀 临界区:每个线程内部访问临界资源的代码片段叫做临界区。 🚀 互斥:任何时刻,互斥保证只有一个指行流进入临界区,访问临界资源,通常是对临界区起保护作用。 🚀 原子性:不被任何调度所打断的操作,该

    2024年02月09日
    浏览(34)
  • Linux——线程的同步与互斥

    目录 模拟抢火车票的过程 代码示例 thread.cc Thread.hpp 运行结果 分析原因 tickets减到-2的本质  解决抢票出错的方案 临界资源的概念 原子性的概念 加锁 定义 初始化 销毁 代码形式如下 代码示例1: 代码示例2: 总结 如何看待锁 申请失败将会阻塞  pthread_mutex_tyrlock 互斥锁实现

    2024年02月06日
    浏览(28)
  • Linux——多线程,互斥与同步

    目录 一.linux互斥 1.进程线程间的互斥相关背景概念 2.互斥量mutex 3.加锁互斥锁mutex 4.锁的底层原理  二.可重入VS线程安全 1.概念 2.常见的线程不安全的情况 3.常见的线程安全的情况  4.常见不可重入的情况  5..常见可重入的情况 6.可重入与线程安全联系  三.死锁 1.死锁四个必

    2024年02月05日
    浏览(25)
  • 『Linux』第九讲:Linux多线程详解(三)_ 线程互斥 | 线程同步

    「前言」文章是关于Linux多线程方面的知识,上一篇是 Linux多线程详解(二),今天这篇是 Linux多线程详解(三),内容大致是线程互斥与线程同步,讲解下面开始! 「归属专栏」Linux系统编程 「主页链接」个人主页 「笔者」枫叶先生(fy) 「枫叶先生有点文青病」「每篇一句

    2024年02月02日
    浏览(62)
  • 【关于Linux中----线程互斥与同步】

    先来用代码模拟一个抢票的场景,四个线程不停地抢票,一共有1000张票,抢完为止,代码如下: 执行结果如下: 可以看到,最后出现了票数为负数的情况,很显然这是错误的,是不应该出现的。 为什么会出现这种情况? 首先要明确,上述的几个线程是不能同时执行抢票的

    2023年04月08日
    浏览(33)
  • 【Linux】多线程2——线程互斥与同步/多线程应用

    💭上文主要介绍了多线程之间的独立资源,本文将详细介绍多线程之间的 共享资源 存在的问题和解决方法。 intro 多线程共享进程地址空间,包括创建的全局变量、堆、动态库等。下面是基于全局变量实现的一个多线程抢票的demo。 发现错误:线程抢到负数编号的票,为什么

    2024年02月10日
    浏览(35)
  • 【Linux】多线程 --- 线程同步与互斥+生产消费模型

    人生总是那么痛苦吗?还是只有小时候是这样? —总是如此 1. 假设现在有一份共享资源tickets,如果我们想让多个线程都对这个资源进行操作,也就是tickets- -的操作,但下面两份代码分别出现了不同的结果,上面代码并没有出现问题,而下面代码却出现了票为负数的情况,这

    2024年02月06日
    浏览(33)
  • Linux pthread线程操作 和 线程同步与互斥操作

    在Linux系统中玩线程,使用pthread,这篇博客记录如何 创建线程 和 使用线程 和线程的 同步 与 互斥 。 还有一份nginx线程池的代码供大家阅读学习! 目录 一、简介 什么是线程 线程的优点、缺点 线程的应用场合 二、线程的使用 1.  创建线程 - pthread_create 2.  线程的终止 - pt

    2024年02月02日
    浏览(24)
  • 【学习笔记】Windows 下线程同步之互斥锁

    本文所涉及的同步主要描述在 Windows 环境下的机制,和 Linux 中的同步机制有一定的联系,但注意并不完全相同。类似于,Windows 和 Linux 按照自己的方式实现了操作系统中同步机制的概念 本文记录的是 Windows 下的互斥锁同步机制,但在 Windows 的同步机制中,其中很多的概念和

    2024年02月06日
    浏览(70)
  • 【Linux】多线程02 --- 线程的同步互斥问题及生产消费模型

    🍎 作者: 阿润菜菜 📖 专栏: Linux系统编程 线程同步互斥问题是指多线程程序中,如何保证共享资源的正确访问和线程间的协作。 因为线程互斥是实现线程同步的基础和前提,我们先讲解线程互斥问题。 在多线程中,假设我们有一个黄牛抢票的代码,其中有一份共享资源

    2024年02月08日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包