Linux _pthread 线程的同步 浅见

2023-12-14 14:48

本文主要是介绍Linux _pthread 线程的同步 浅见,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

线程的同步

  1. 问题
    同一个进程内的各个线程,共享该进程内的全局变量
    如果多个线程同时对某个全局变量进行访问时,就可能导致竞态。

    解决办法,对临界区使用信号量、或互斥量。

  2. 信号量和互斥量的选择。
    对于同步和互斥,使用信号量或互斥量都可以实现。
    使用时,选择更符合语义的手段:
    如果要求最多只允许一个线程进入临界区,则使用互斥量
    如果要求多个线程之间的执行顺序满足某个约束,则使用信号量

  3. 信号量
    1)什么是信号量
    此时所指的“信号量”是指用于同一个进程内多个线程之间的信号量。
    即POSIX信号量,而不是System V信号量(用于进程之间的同步)

     用于线程的信号量的原理,与用于进程之间的信号量的原理相同。都有P操作、V操作。信号量的表示:sem_t  类型
    

    2) 信号量的初始化
    sem_init
    原型:int sem_init (sem_t *sem, int pshared, unsigned int value);
    功能:对信号量进行初始化
    参数:sem, 指向被初始化的信号量
    pshared, 0:表示该信号量是该进程内使用的“局部信号量”, 不再被其他进程共享。
    非0:该信号量可被其他进程共享,Linux不支持这种信号量
    value, 信号量的初值。

    = 0
    返回值:成功,返回0
    失败, 返回错误码

    3) 信号量的P操作
    sem_wait
    原型:int sem_wait (sem_t *sem);
    返回值:成功,返回0
    失败, 返回错误码

    4) 信号量的V操作
    sem_post
    原型:int sem_post (sem_t *sem);
    返回值:成功,返回0
    失败, 返回错误码

    5) 信号量的删除
    sem_destroy
    原型:int sem_destroy (sem_t *sem);
    返回值:成功,返回0
    失败, 返回错误码

    6) 实例
    主线程循环输入字符串,把字符串存放到一个全局缓存中。
    新线程从全局缓存中读取字符串,统计该字符串的长度。
    直到用户输入end
    main1.c

    创建2个线程(共有主线程、线程1、线程2共3个线程)
    主线程阻塞式等待用户输入字符串
    主线程每接收到一个字符串之后, 线程1就马上对该字符串进行处理。
    线程1的处理逻辑为:统计该字符串的个数,并记录当时的时间。
    线程1把该字符串处理完后,线程2马上就把处理结果写入文件result.txt
    直到用户输入exit.
    multi_pthread.c
    
  4. 互斥量
    1)什么是互斥量
    效果上等同于初值为1的信号量

     互斥量的使用:类型为 pthread_mutex_t
    

    2)互斥量的初始化
    pthread_mutex_init
    原型:int pthread_mutex_init(pthread_mutex_t *mutex,
    pthread_mutexattr_t *attr);
    参数:mutex, 指向被初始化的互斥量
    attr, 指向互斥量的属性
    一般取默认属性(当一个线程已获取互斥量后,该线程再次获取该信号量,将导致死锁!)

    3) 互斥量的获取
    pthread_mutex_lock
    原型:int pthread_mutex_lock (pthread_mutex_t *mutex);

    4)互斥量的释放
    pthread_mutex_unlock
    原型:int pthread_mutex_unlock (pthread_mutex_t *mutex);

    5)互斥量的删除
    pthread_mutex_destroy
    int pthread_mutex_destroy (pthread_mutex_t *mutex);

    6) 实例
    最简单的互斥量使用
    main2.c

    main3.c
    (1) 分析程序的功能
    (2) 分析程序存在的隐患
    (3) 用互斥量解决该程序的隐患 (不能删除、修改代码,只能添加代码)         修改后为main4.c
    
void work(char *str)
{char *p = str;while(*p) {if (*p >= 'a' && *p <= 'z') {*p = *p - 'a' + 'A';}}}time_t time;
time(&time);
char *p = ctime(&time);

main1.c

#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>#define BUFF_SIZE 80char buff[BUFF_SIZE];
sem_t sem;static void* str_thread_handle(void *arg) 
{while(1) {//P(sem)if (sem_wait(&sem) != 0) {printf("sem_wait failed!\n");exit(1);}printf("string is: %slen=%d\n", buff, strlen(buff));if (strncmp(buff, "end", 3) == 0) {break;}}
}int main(void)
{int ret;pthread_t  str_thread;void *thread_return;ret = sem_init(&sem, 0, 0);if (ret != 0) {printf("sem_init failed!\n");exit(1);}ret = pthread_create(&str_thread, 0, str_thread_handle, 0);if (ret != 0) {printf("pthread_create failed!\n");exit(1);}while (1) {fgets(buff, sizeof(buff), stdin);//V(sem)if (sem_post(&sem) != 0) {printf("sem_post failed!\n");exit(1);}if (strncmp(buff, "end", 3) == 0) {break;}}ret = pthread_join(str_thread, &thread_return);if (ret != 0) {printf("pthread_join failed!\n");exit(1);}ret = sem_destroy(&sem);if (ret != 0) {printf("sem_destroy failed!\n");exit(1);}return 0;
}

main2.c

#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>#define BUFF_SIZE 80int global_value = 1000;
pthread_mutex_t  lock;static void* str_thread_handle(void *arg) 
{int i = 0;for (i=0; i<10; i++) {//pthread_mutex_lock(&lock);if (global_value  > 0) {// worksleep(1);printf("soled ticket(%d) to ChildStation(%d)\n",global_value, i+1);}global_value--;//pthread_mutex_unlock(&lock);sleep(1);}
}int main(void)
{int ret;pthread_t  str_thread;void *thread_return;int i;ret = pthread_mutex_init(&lock, 0);if (ret != 0) {printf("pthread_mutex_init failed!\n");exit(1);}ret = pthread_create(&str_thread, 0, str_thread_handle, 0);if (ret != 0) {printf("pthread_create failed!\n");exit(1);}for (i=0; i<10; i++) {//pthread_mutex_lock(&lock);if (global_value  > 0) {// worksleep(1);printf("soled ticket(%d) to MainStation(%d)\n",global_value, i+1);}global_value--;//pthread_mutex_unlock(&lock);sleep(1);}ret = pthread_join(str_thread, &thread_return);if (ret != 0) {printf("pthread_join failed!\n");exit(1);}ret = pthread_mutex_destroy(&lock);if (ret != 0) {printf("pthread_mutex_destroy failed!\n");exit(1);}return 0;
}

main3.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>void *thread_function(void *arg);#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;int main() 
{int res;pthread_t a_thread;void *thread_result;res = pthread_create(&a_thread, NULL, thread_function, NULL);if (res != 0) {perror("Thread creation failed");exit(EXIT_FAILURE);}printf("Input some text. Enter 'end' to finish\n");while(!time_to_exit) {fgets(work_area, WORK_SIZE, stdin);while(1) {if (work_area[0] != '\0') {sleep(1);}else {break;}}}printf("\nWaiting for thread to finish...\n");res = pthread_join(a_thread, &thread_result);if (res != 0) {perror("Thread join failed");exit(EXIT_FAILURE);}printf("Thread joined\n");exit(EXIT_SUCCESS);
}void *thread_function(void *arg) 
{sleep(1);while(strncmp("end", work_area, 3) != 0) {printf("You input %d characters\n", strlen(work_area) -1);work_area[0] = '\0';sleep(1);while (work_area[0] == '\0' ) {sleep(1);}}time_to_exit = 1;work_area[0] = '\0';pthread_exit(0);
}

main4.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>void *thread_function(void *arg);
pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;int main() {int res;pthread_t a_thread;void *thread_result;res = pthread_mutex_init(&work_mutex, NULL);if (res != 0) {perror("Mutex initialization failed");exit(EXIT_FAILURE);}res = pthread_create(&a_thread, NULL, thread_function, NULL);if (res != 0) {perror("Thread creation failed");exit(EXIT_FAILURE);}pthread_mutex_lock(&work_mutex);printf("Input some text. Enter 'end' to finish\n");while(!time_to_exit) {fgets(work_area, WORK_SIZE, stdin);pthread_mutex_unlock(&work_mutex);while(1) {pthread_mutex_lock(&work_mutex);if (work_area[0] != '\0') {pthread_mutex_unlock(&work_mutex);sleep(1);}else {break;}}}pthread_mutex_unlock(&work_mutex);printf("\nWaiting for thread to finish...\n");res = pthread_join(a_thread, &thread_result);if (res != 0) {perror("Thread join failed");exit(EXIT_FAILURE);}printf("Thread joined\n");pthread_mutex_destroy(&work_mutex);exit(EXIT_SUCCESS);
}void *thread_function(void *arg) {sleep(1);pthread_mutex_lock(&work_mutex);while(strncmp("end", work_area, 3) != 0) {printf("You input %d characters\n", strlen(work_area) -1);work_area[0] = '\0';pthread_mutex_unlock(&work_mutex);sleep(1);pthread_mutex_lock(&work_mutex);while (work_area[0] == '\0' ) {pthread_mutex_unlock(&work_mutex);sleep(1);pthread_mutex_lock(&work_mutex);}}time_to_exit = 1;work_area[0] = '\0';pthread_mutex_unlock(&work_mutex);pthread_exit(0);
}

这篇关于Linux _pthread 线程的同步 浅见的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/492949

相关文章

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

Linux实现线程同步的多种方式汇总

《Linux实现线程同步的多种方式汇总》本文详细介绍了Linux下线程同步的多种方法,包括互斥锁、自旋锁、信号量以及它们的使用示例,通过这些同步机制,可以解决线程安全问题,防止资源竞争导致的错误,示例... 目录什么是线程同步?一、互斥锁(单人洗手间规则)适用场景:特点:二、条件变量(咖啡厅取餐系统)工作流

Java中常见队列举例详解(非线程安全)

《Java中常见队列举例详解(非线程安全)》队列用于模拟队列这种数据结构,队列通常是指先进先出的容器,:本文主要介绍Java中常见队列(非线程安全)的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一.队列定义 二.常见接口 三.常见实现类3.1 ArrayDeque3.1.1 实现原理3.1.2

SpringBoot3中使用虚拟线程的完整步骤

《SpringBoot3中使用虚拟线程的完整步骤》在SpringBoot3中使用Java21+的虚拟线程(VirtualThreads)可以显著提升I/O密集型应用的并发能力,这篇文章为大家介绍了详细... 目录1. 环境准备2. 配置虚拟线程方式一:全局启用虚拟线程(Tomcat/Jetty)方式二:异步