互斥锁/信号量实现5个线程同步

news/2025/2/1 17:04:28/

互斥锁 实现同步 

互斥锁保证在同一时刻,只有一个线程可以访问共享资源,从而实现了线程同步。  

思路 

1 创建互斥锁(1个)
        pthread_mutex_t    mutex;
2 初始化互斥锁
        所有线程开始执行前,pthread_mutex_init(&mutex, NULL);
3 创建线程(5个)
        pthread_create(&threads[i], NULL, thread_function, arg);
        每个线程函数,临界区开始处用 pthread_mutex_lock 加锁,
        结束用 pthread_mutex_unlock 解锁。
4 等待 threads[i] 结束
        pthread_join(threads[i], NULL);
5 销毁互斥锁
        pthread_mutex_destroy(&mutex);

代码 

#define NUM_THREADS 5pthread_mutex_t mutex;//创建一个互斥锁void *thread_function(void *arg) {int thread_id = *(int *)arg;free(arg);pthread_mutex_lock(&mutex);printf("线程%d\n", thread_id);pthread_mutex_unlock(&mutex);return NULL;
}int main() {pthread_t threads[NUM_THREADS];// 初始化互斥锁pthread_mutex_init(&mutex, NULL);//创建5个线程for (int i = 0; i < NUM_THREADS; i++) {int *arg = malloc(sizeof(int));*arg = i;pthread_create(&threads[i], NULL, thread_function, arg);}//等待 threads[i] 结束for (int i = 0; i < NUM_THREADS; i++) {pthread_join(threads[i], NULL);}// 销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}

运行结果 

 

信号量 实现同步 

互斥锁适用于需要严格互斥的情况,而信号量则提供了更灵活的同步控制。

思路

1 创建信号量(1个)
        sem_t semaphore;
2 初始化信号量,初始值为1
        sem_init(&semaphore, 0, 1);
3 创建线程(5个)
        pthread_create(&threads[i], NULL, thread_function, arg);
        每个线程函数,临界区开始处用  sem_wait 减少信号量的值,阻塞线程直到值大于0。
        结束处用 sem_post 增加信号量的值,从而允许其他线程进入临界区。
4 等待 threads[i] 结束
        pthread_join(threads[i], NULL);
5 销毁信号量
        sem_destroy(&semaphore);

代码

#define NUM_THREADS 5sem_t semaphore;//创建信号量void *thread_function(void *arg) {int thread_id = *(int *)arg;free(arg);sem_wait(&semaphore);printf("线程%d\n", thread_id);sem_post(&semaphore);return NULL;
}int main() {pthread_t threads[NUM_THREADS];// 初始化信号量,初始值为1sem_init(&semaphore, 0, 1);、//创建5个线程for (int i = 0; i < NUM_THREADS; i++) {int *arg = malloc(sizeof(int));*arg = i;pthread_create(&threads[i], NULL, thread_function, arg);}//等待 threads[i] 结束for (int i = 0; i < NUM_THREADS; i++) {pthread_join(threads[i], NULL);}// 销毁信号量sem_destroy(&semaphore);return 0;
}

运行结果 

补充说明

信号量的初始值决定了可以同时访问共享资源的线程数量。
设置初始值为1,意味着信号量表现得像互斥锁。
如果需要允许多个线程同时访问,可以设置一个更大的初始值


http://www.ppmy.cn/news/1568469.html

相关文章

【ARM】解决MDK在打开工程的时候提示CMSIS的版本不对问题

1、 文档目标 解决MDK在打开使用Compiler 6的工程的时候&#xff0c;提示CMSIS 的API版本过低的报错。 2、 问题场景 客户在Pack包中打开一个示例工程&#xff0c;打算熟悉一下对应芯片的功能和软件的功能&#xff0c;但是&#xff0c;打开软件后&#xff0c;在构建信息输出框…

Skynet实践之「Lua C 模块集成—优先级队列」

本文展示了 如何以 C 实现一个通用的“最小堆&#xff08;Min-Heap&#xff09;优先队列 并在 Skynet 中通过 Lua C 模块集成使用&#xff0c;从而获得更高的性能与通用性。 一、C 语言模块&#xff1a;cpriorityqueue.c 以下代码演示了一个最小堆的数据结构&#xff08;以 ru…

用HTML、CSS和JavaScript实现庆祝2025蛇年大吉(附源码)

用HTML、CSS和JavaScript庆祝2025蛇年大吉 在这个数字化时代&#xff0c;网页设计不仅仅是为了展示信息&#xff0c;更是传达情感和文化的一种方式。2025年将是蛇年&#xff0c;许多人希望通过各种方式庆祝这一重要的时刻。在这篇文章中&#xff0c;我们将一起学习如何使用HTM…

fpga系列 HDL:verilog 常见错误与注意事项 quartus13 bug 初始失效 reg *** = 1;

代码 顶层模块 module bug(input wire clk,output wire BitOut );reg BitIn 1;Encoder encoder (.clk(clk),.BitIn(BitIn),.BitOut(BitOut) ); endmodule内部逻辑模块 module Encoder(input wire clk,input wire BitIn,output reg BitOut );always (posedge clk) beginif…

Elasticsearch——Elasticsearch性能优化实战

摘要 本文主要介绍了 Elasticsearch 性能优化的实战方法&#xff0c;从硬件配置优化、索引优化设置、查询方面优化、数据结构优化以及集群架构设计等五个方面进行了详细阐述&#xff0c;旨在帮助读者提升 Elasticsearch 的性能表现。 1. 硬件配置优化 升级硬件设备配置一直都…

讯飞绘镜(ai生成视频)技术浅析(三):自然语言处理(NLP)

1. 技术架构概述 讯飞绘镜的 NLP 技术架构可以分为以下几个核心模块: 语义分析:理解用户输入的文本,提取关键信息(如实体、事件、情感等)。 情节理解:分析文本中的故事情节,识别事件序列和逻辑关系。 人物关系建模:识别文本中的人物及其关系,构建人物关系图。 场景生…

Mac Electron 应用签名(signature)和公证(notarization)

在MacOS 10.14.5之后&#xff0c;如果应用没有在苹果官方平台进行公证notarization(我们可以理解为安装包需要审核&#xff0c;来判断是否存在病毒)&#xff0c;那么就不能被安装。当然现在很多人的解决方案都是使用sudo spctl --master-disable&#xff0c;取消验证模式&#…

新鲜速递:DeepSeek-R1开源大模型本地部署实战—Ollama + MaxKB 搭建RAG检索增强生成应用

在AI技术快速发展的今天&#xff0c;开源大模型的本地化部署正在成为开发者们的热门实践方向。最火的莫过于吊打OpenAI过亿成本的纯国产DeepSeek开源大模型&#xff0c;就在刚刚&#xff0c;凭一己之力让英伟达大跌18%&#xff0c;纳斯达克大跌3.7%&#xff0c;足足是给中国AI产…