• 20220920线程属性


     互斥锁

    使用mutex(互斥量)一般步骤
    1.pthread_mutex_t mutex;创建锁
    2.pthread_mutex_init:初始化
    3.pthread_mutex_lock:加锁
    4访问共享数据(stdout)
    5.pthread_mutex_unlock:解锁
    6.pthread_mutex_destroy:销毁锁

    1. #include <stdio.h>
    2. #include <string.h>
    3. #include <pthread.h>
    4. #include <stdlib.h>
    5. #include <unistd.h>
    6. //pthread_mutex_t 互斥锁类型名,
    7. pthread_mutex_t mutex;//定义另一个互斥锁变量mutex,全局变量,所有线程可以共享使用
    8. void *tfn2(void *arg)
    9. {//当某个线程,不进行对共享资源进行加锁操作时,仍旧会造成抢夺资源现象
    10. srand(time(NULL));
    11. while(1)
    12. {
    13. int ret=pthread_mutex_trylock(&mutex);
    14. if(ret!=0)
    15. {
    16. printf("pthread_mutx_trylock err:%s\n",strerror(ret));
    17. }
    18. else
    19. {
    20. printf("---tfn2()---print ");
    21. sleep(rand() % 3);
    22. printf("to stdout\n");
    23. sleep(rand() % 3);
    24. ret=pthread_mutex_unlock(&mutex);//解锁
    25. if(ret!=0)
    26. {
    27. fprintf(stderr,"pthread_mutex_unlock() error:%s\n",strerror(ret));
    28. }
    29. }
    30. printf("common tfn2()------\n");
    31. sleep(rand() % 3);
    32. }
    33. }
    34. void *tfn(void *arg)
    35. {
    36. srand(time(NULL));
    37. while (1)
    38. {
    39. int ret=pthread_mutex_lock(&mutex);//加锁
    40. if(ret!=0)
    41. {
    42. fprintf(stderr,"pthread_mutex_lock() error:%s\n",strerror(ret));
    43. }
    44. printf("hello ");
    45. sleep(rand() % 3);
    46. printf("world\n");
    47. //pthread_mutex_unlock(),对加锁成功的共享资源进行解锁
    48. ret=pthread_mutex_unlock(&mutex);//解锁
    49. if(ret!=0)
    50. {
    51. fprintf(stderr,"pthread_mutex_unlock() error:%s\n",strerror(ret));
    52. }
    53. sleep(rand() % 3);
    54. }
    55. return NULL;
    56. }
    57. int main(void)
    58. {
    59. pthread_t tid;
    60. srand(time(NULL));
    61. //pthread_mutex_init()初始化 互斥锁
    62. int ret=pthread_mutex_init(&mutex,NULL);//初始化锁
    63. if(ret!=0)
    64. {
    65. fprintf(stderr,"mutex init error:%s\n",strerror(ret));
    66. }
    67. //pthread_create() 创建线程1
    68. ret=pthread_create(&tid, NULL, tfn, NULL);
    69. if(ret!=0)
    70. {
    71. fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
    72. }
    73. //pthread_create() 创建线程2
    74. ret=pthread_create(&tid, NULL, tfn2, NULL);
    75. if(ret!=0)
    76. {
    77. fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
    78. }
    79. while (1)
    80. { //pthread_mutex_lock()给已初始化过的互斥锁变量,(站在当前线程角度)对共享资源进行加锁操作
    81. //如果加锁失败,则阻塞,也就是一直等待
    82. ret=pthread_mutex_lock(&mutex);//加锁
    83. if(ret!=0)
    84. {
    85. fprintf(stderr,"pthread_mutex_lock() error:%s\n",strerror(ret));
    86. }
    87. printf("HELLO ");
    88. sleep(rand() % 3);
    89. printf("WORLD\n");
    90. //pthread_mutex_unlock(),对加锁成功的共享资源进行解锁
    91. ret=pthread_mutex_unlock(&mutex);//解锁
    92. if(ret!=0)
    93. {
    94. fprintf(stderr,"pthread_mutex_unlock() error:%s\n",strerror(ret));
    95. }
    96. sleep(rand() % 3);
    97. }
    98. pthread_join(tid, NULL);
    99. ret=pthread_mutex_destroy(&mutex);//销毁锁
    100. if(ret!=0)
    101. {
    102. fprintf(stderr,"pthread_mutex_destroy() error:%s\n",strerror(ret));
    103. }
    104. return 0;
    105. }

    读写锁

    特别强调,读写锁只有一把,但具备两种状态:
    1读模式下加锁状态(读锁)
    2.写模式下加锁状态(写锁)

    特征:
    1.读写锁是“写模式加锁”成功时,其它线程加锁都会被阻塞
    2.读写锁“读模式加锁”成功时,其他线程以写模式加锁会阻塞
    3.读锁、写锁并行阻塞,写锁优先级高。读锁也叫共享

    1. #include <stdio.h>
    2. #include <unistd.h>
    3. #include <pthread.h>
    4. int counter=0;
    5. //用pthread_rwlock_t类型,定义读写锁变量rwlock
    6. pthread_rwlock_t rwlock;
    7. /* 3个线程不定时写同一全局资源,5个线程不定时读同一全局资源*/
    8. void *th_write(void *arg)
    9. {
    10. int t;
    11. int i = (long int)arg;
    12. while (1)
    13. {
    14. pthread_rwlock_wrlock(&rwlock);//以写模式加锁
    15. t = counter;
    16. counter++;
    17. // sleep(1);
    18. printf("=====write i=%d:tid=%lu:counter=%d ++counter=%d\n", i, pthread_self(), t, counter);
    19. pthread_rwlock_unlock(&rwlock);
    20. sleep(1);
    21. }
    22. return NULL;
    23. }
    24. void *th_read(void *arg)
    25. {
    26. int i = (long int)arg;
    27. while (1)
    28. {
    29. pthread_rwlock_rdlock(&rwlock);//读线程间,读锁共享
    30. printf("----------------------read i=%d:tid=%lu:counter=%d\n", i, pthread_self(), counter);
    31. pthread_rwlock_unlock(&rwlock);
    32. usleep(100000);
    33. }
    34. return NULL;
    35. }
    36. int main(void)
    37. {
    38. long int i;
    39. pthread_t tid[8];
    40. pthread_rwlock_init(&rwlock, NULL);
    41. for(i=0;i<3;i++)
    42. {
    43. pthread_create(&tid[i], NULL, th_write, (void *)i);
    44. }
    45. for(i=0;i<5;i++)
    46. {
    47. pthread_create(&tid[i+3], NULL, th_read, (void *)i);
    48. }
    49. for(i=0;i<8;i++)
    50. {
    51. pthread_join(tid[i], NULL);
    52. }
    53. pthread_rwlock_destroy(&rwlock);
    54. return 0;
    55. }

  • 相关阅读:
    Redis未授权访问漏洞实验
    ts重点学习144-描述文件声明
    TIA博途中已经被调用的变量,为什么交叉引用时却没有显示调用信息?
    浅谈Vue组件开发几个原则
    Cesium之home键开关及相机位置设置
    阿里云国际站扩容云盘扩容分区和文件系统(Linux)怎么操作?
    千亿播放量!年轻人又迷上了这些解压方式!!!
    联创电子业绩惊现首亏:主力产品下滑,车载业务扛不住了?
    mybatis trim标签使用详解
    【CMU15-445数据库】bustub Project #1:Buffer Pool
  • 原文地址:https://blog.csdn.net/liuasdfgqwer/article/details/126960291