嵌入式 互斥锁和读写锁区别

简介:   /*  * 线程同步——互斥量  * 创建两个线程,使用互斥量使任一时刻只有一个线程对全局变量进行 操作  * Lzy 2011-6-19  */ #include  #include  #include  pthread_mutex_t mutex;   ...
 
  1. /*
  2.  * 线程同步——互斥量
  3.  * 创建两个线程,使用互斥量使任一时刻只有一个线程对全局变量进行
  4. 操作
  5.  * Lzy 2011-6-19
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10. pthread_mutex_t mutex;                    /* 定义
  11. 互斥量 */
  12. int x;                                
  13.     /* 定义全局变量 */
  14. void thread1(void)                /* 定义线程1运
  15. 行的函数,其功能是对全局变量x进行逐减操作 */
  16. {
  17.   while(x>0)
  18.   {
  19.     pthread_mutex_lock(&mutex);            /* 对互斥量进行
  20. 加锁操作 */
  21.     printf("Thread 1 is running : x=%d \n",x);
  22.     x--;
  23.     pthread_mutex_unlock(&mutex);        /* 对互斥量进行
  24. 解锁操作 */
  25.     sleep(1);
  26.   }
  27.   pthread_exit(NULL);
  28. }
  29. void thread2(void)                /* 定义线程2运
  30. 行的函数,功能与thread2相同 */
  31. {
  32.   while(x>0)
  33.   {
  34.     pthread_mutex_lock(&mutex);            /* 对互斥量进行
  35. 加锁操作 */
  36.     printf("Thread 2 is running : x=%d \n",x);
  37.     x--;
  38.     pthread_mutex_unlock(&mutex);        /* 对互斥量进行
  39. 解锁操作 */
  40.     sleep(1);
  41.   }
  42.   pthread_exit(NULL);
  43. }
  44. int main(void)
  45. {
  46.   pthread_t id1,id2;                        
  47. /* 定义线程的标识符 */
  48.   int ret;
  49.   ret = pthread_mutex_init(&mutex,NULL);    /* 对互斥量进行
  50. 初始化,这里使用默认的属性 */
  51.   if(ret != 0)
  52.   {
  53.     printf ("Mutex initialization failed.\n");        /* 如果
  54. 初始化失败,打印错误信息 */
  55.     exit (1);
  56.   }
  57.   x=10;                                
  58. /* 对全局变量赋初值 */
  59.   ret = pthread_create(&id1, NULL, (void *)&thread1, NULL);    
  60.     /* 创建线程1 */
  61.   if(ret != 0)
  62.   {
  63.     printf ("Thread1 creation failed.\n");
  64.     exit (1);
  65.   }
  66.   ret = pthread_create(&id2, NULL, (void *)&thread2, NULL);    
  67.     /* 创建线程2 */
  68.   if(ret != 0)
  69.   {
  70.     printf ("Thread2 creation failed.\n");
  71.     exit (1);
  72.   }
  73.   pthread_join(id1, NULL);                /*线程
  74. 合并 */
  75.   pthread_join(id2, NULL);
  76.   return (0);
  77. }
  1. /*
  2.  * 线程同步
  3.  * ——读写锁 
  4.  *     只要没有进程持有某个给定的读写锁用于写,那么任意数目的
  5. 线程都可持有该读写锁用于读
  6.  *     仅当没有线程持有某个给定的读写锁用于读或写,才能分配该
  7. 读写锁用于写。
  8.  * Lzy 2011-6-19
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <pthread.h>
  13. int product = 0;        //定义全局变量
  14. pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;    //静态
  15. 初始化读写锁    
  16. void * threadRead(void * arg)            //线程函数读 
  17. {
  18.     int cnt = 0;
  19.     while(cnt++ < 100)
  20.     {
  21.         pthread_rwlock_rdlock(&rwlock);    //读锁
  22.         
  23.         printf("Read: product = %d\n", product);
  24.         
  25.         pthread_rwlock_unlock(&rwlock);    //解锁
  26.         sleep(1);
  27.     }
  28. }
  29. void * tidProduce(void * arg)    //线程函数写 加1
  30. {
  31.     int cnt = 0;
  32.     while(cnt++ < 100)
  33.     {
  34.         pthread_rwlock_wrlock(&rwlock);    //写锁
  35.         
  36.         product++;
  37.         printf("Produce: product = %d\n", product);
  38.         
  39.         pthread_rwlock_unlock(&rwlock);    //解锁
  40.         sleep(1);
  41.     }
  42. }
  43. void * threadConsume(void * arg)    //线程函数写 减1
  44. {
  45.     int cnt = 0;
  46.     while(cnt++ < 100)
  47.     {
  48.         pthread_rwlock_wrlock(&rwlock);    //写锁
  49.         
  50.         product--;
  51.         printf("Consume: product = %d\n", product);
  52.         
  53.         pthread_rwlock_unlock(&rwlock);    //解锁
  54.         sleep(2);
  55.     }
  56. }
  57. int main(void)
  58. {
  59.     int i;
  60.     pthread_t tid[10], tidconsume, tidproduce;
  61.     
  62.     for(i = 0; i < 2; i++)
  63.     {
  64.         if(pthread_create(&tid[i], NULL, threadRead, 
  65. NULL))
  66.         {
  67.             printf("pthread_create error\n");
  68.             exit(0);
  69.         }
  70.     }
  71.     if(pthread_create(&tidproduce, NULL, tidProduce, NULL))
  72.     {
  73.         printf("pthread_create error\n");
  74.         exit(0);
  75.     }
  76.     if(pthread_create(&tidconsume, NULL, threadConsume, 
  77. NULL))
  78.     {
  79.         printf("pthread_create error\n");
  80.         exit(0);
  81.     }
  82.     pthread_exit(NULL);        //等待所有线程结束 
  83.     return 0;
  84. }
目录
相关文章
|
7月前
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课15 集成DeepSeek等大模型
本文介绍了如何在PolarDB数据库中接入私有化大模型服务,以实现多种应用场景。实验环境依赖于Docker容器中的loop设备模拟共享存储,具体搭建方法可参考相关系列文章。文中详细描述了部署ollama服务、编译并安装http和openai插件的过程,并通过示例展示了如何使用这些插件调用大模型API进行文本分析和情感分类等任务。此外,还探讨了如何设计表结构及触发器函数自动处理客户反馈数据,以及生成满足需求的SQL查询语句。最后对比了不同模型的回答效果,展示了deepseek-r1模型的优势。
324 3
|
文字识别 并行计算 JavaScript
PaddleOCR + Django 实现一个OCR在线识别网站,一起来玩呀
除了PaddleOCR之外,之前还介绍过一些其它好玩的开源项目,例如老照片修复 Bringing-Old-Photos-Back-to-Life 、黑白照片上色DeOldify 。因此,最近准备启动一个项目,做一个在线网站,将之前一些好玩的功能都陆续集成在这个网站中
PaddleOCR + Django 实现一个OCR在线识别网站,一起来玩呀
|
分布式计算 监控 Spark
spark 3.x Plugin Framework
spark 3.x Plugin Framework
403 0
|
前端开发 JavaScript 安全
常用前端开源静态网站推荐
常用前端开源静态网站推荐
824 0
常用前端开源静态网站推荐
|
存储 Java Maven
maven的安装教程以及nexus私服配置
建立自己的maven仓库,方便快捷Java开发
maven的安装教程以及nexus私服配置
|
XML 开发框架 监控
Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
|
Java 应用服务中间件 开发工具
功能开关最佳实践
功能开关是一个轻量级的动态配置框架,通过功能开关可以动态管理代码中的配置项,根据需求为某个应用开启或关闭部分功能,或设置某个性能指标的阈值。功能开关通常用于设置黑白名单、运行时动态调整日志级别、降级业务功能等场景。本文介绍最佳实践。
602 1
|
存储 数据采集 边缘计算
深度揭秘:一汽红旗新能源工厂为何能“1分钟造1辆车”
年产能20万辆车——平均一分钟就能造一辆车,这就是去年投产的一汽红旗新能源工厂的效率。这个超乎想象的造车节奏,是如何产生的呢?全自动生产线的天花板又是如何被突破的呢?
1461 1
深度揭秘:一汽红旗新能源工厂为何能“1分钟造1辆车”
|
Java 数据库连接 API
深入了解数据校验(Bean Validation):ValidatorFactory和Validator等核心API【享学Java】(上)
深入了解数据校验(Bean Validation):ValidatorFactory和Validator等核心API【享学Java】(上)
深入了解数据校验(Bean Validation):ValidatorFactory和Validator等核心API【享学Java】(上)
|
Kubernetes 安全 Ubuntu
BTFGen: 让 eBPF 程序可移植发布更近一步
BTFGen: 让 eBPF 程序可移植发布更近一步
1512 0