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

简介:   /*  * 线程同步——互斥量  * 创建两个线程,使用互斥量使任一时刻只有一个线程对全局变量进行 操作  * 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. }
目录
相关文章
|
4月前
|
API 调度 C语言
互斥锁,自旋锁,原子操作的原理,区别和实现
v互斥锁,自旋锁,原子操作的原理,区别和实现
27 0
|
30天前
|
传感器 安全 程序员
【C++多线程 同步机制】:探索 从互斥锁到C++20 同步机制的进化与应用
【C++多线程 同步机制】:探索 从互斥锁到C++20 同步机制的进化与应用
92 1
|
3月前
|
Linux C语言
linux c 多线程 互斥锁、自旋锁、原子操作的分析与使用
生活中,我们常常会在12306或者其他购票软件上买票,特别是春节期间或者国庆长假的时候,总会出现抢票的现象,最后总会有人买不到票而埋怨这埋怨那,其实这还好,至少不会跑去现场或者网上去找客服理论,如果出现了付款,但是却没买到票的现象,那才是真的会出现很多问题,将这里的票引入到多线程中,票就被称为临界资源。
29 0
|
3月前
|
安全 Linux 调度
Linux C/C++ 开发(学习笔记四):多线程并发锁:互斥锁、自旋锁、原子操作、CAS
Linux C/C++ 开发(学习笔记四):多线程并发锁:互斥锁、自旋锁、原子操作、CAS
41 0
|
4月前
|
存储 缓存 Linux
Linux驱动开发(锁和信号量的概念及实现原理)
Linux驱动开发(锁和信号量的概念及实现原理)
50 0
|
7月前
|
调度 C++
如何使用C++11原子操作实现自旋锁
C++自旋锁是一种低层次的同步原语,用于保护共享资源的访问。自旋锁是一种轻量级的锁,适用于短时间的资源锁定。
154 1
|
8月前
|
Java
【并发技术14】线程同步工具Semaphore的使用
【并发技术14】线程同步工具Semaphore的使用
|
9月前
|
Linux 编译器
Linux系统应用编程---线程同步基础(互斥量、死锁、读写锁)
Linux系统应用编程---线程同步基础(互斥量、死锁、读写锁)
92 0
|
Linux
Linux驱动开发——并发和竞态(信号量方式的使用④)
Linux驱动开发——并发和竞态(信号量方式的使用④)
127 0
Linux驱动开发——并发和竞态(信号量方式的使用④)
|
Linux
Linux驱动开发——并发和竞态(原子操作方式的使用⑤)
Linux驱动开发——并发和竞态(原子操作方式的使用⑤)
115 0
Linux驱动开发——并发和竞态(原子操作方式的使用⑤)