zoukankan      html  css  js  c++  java
  • 嵌入式 互斥锁和读写锁区别

     
    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 ",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 ",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. ");        /* 如果
    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. ");
    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. ");
    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 ", 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 ", 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 ", 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 ");
    68.             exit(0);
    69.         }
    70.     }
    71.     if(pthread_create(&tidproduce, NULL, tidProduce, NULL))
    72.     {
    73.         printf("pthread_create error ");
    74.         exit(0);
    75.     }
    76.     if(pthread_create(&tidconsume, NULL, threadConsume, 
    77. NULL))
    78.     {
    79.         printf("pthread_create error ");
    80.         exit(0);
    81.     }
    82.     pthread_exit(NULL);        //等待所有线程结束 
    83.     return 0;
    84. }
  • 相关阅读:
    POJ3122贪心或者二分(分蛋糕)
    POJ2118基础矩阵快速幂
    POJ2118基础矩阵快速幂
    POJ1328贪心放雷达
    POJ1328贪心放雷达
    hdu4642博弈(矩阵)
    hdu4642博弈(矩阵)
    POJ1042 贪心钓鱼
    POJ3160强连通+spfa最长路(不错)
    POJ3114强连通+spfa
  • 原文地址:https://www.cnblogs.com/lidabo/p/5382657.html
Copyright © 2011-2022 走看看