东坡网 - 更多系统软件下载,请关注东坡网:www.dongpow.com

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

线程同步的方法有哪些?Linux下实现线程同步的三种方法

时间:2022-03-05来源:东坡网作者:chunhua

  线程同步的方法有哪些?在linux下,系统提供了很多种方式来实现线程同步,其中最常用的便是互斥锁、条件变量和信号量这三种方式,可能还有很多伙伴对于这三种方法都不熟悉,下面就给大家详细介绍下。

线程同步的方法有哪些?Linux下实现线程同步的三种方法

  Linux下实现线程同步的三种方法:

  一、互斥锁(mutex)

  通过锁机制实现线程间的同步。

  1、初始化锁。在Linux下,线程的互斥量数据类型是pthread_mutex_t。在使用前,要对它进行初始化。

  静态分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

  动态分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);

  2、加锁。对共享资源的访问,要对互斥量进行加锁,如果互斥量已经上了锁,调用线程会阻塞,直到互斥量被解锁。

  int pthread_mutex_lock(pthread_mutex *mutex);

  int pthread_mutex_trylock(pthread_mutex_t *mutex);

  3、解锁。在完成了对共享资源的访问后,要对互斥量进行解锁。

  int pthread_mutex_unlock(pthread_mutex_t *mutex);

  4、销毁锁。锁在是使用完成后,需要进行销毁以释放资源。

  int pthread_mutex_destroy(pthread_mutex *mutex);

  1. 01#include <stdlib.h>
  2. 02#include <stdio.h>
  3. 03#include <unistd.h>
  4. 04#include <pthread.h>
  5. 05#include <semaphore.h>
  6. 06#include <errno.h>
  7. 07#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}
  8. 08typedef struct _PrivInfo
  9. 09{
  10. 10sem_t s1;
  11. 11sem_t s2;
  12. 12time_t end_time;
  13. 13}PrivInfo;
  14. 14static void info_init (PrivInfo* thiz);
  15. 15static void info_destroy (PrivInfo* thiz);
  16. 16static void* pthread_func_1 (PrivInfo* thiz);
  17. 17static void* pthread_func_2 (PrivInfo* thiz);
  18. 18int main (int argc, char** argv)
  19. 19{
  20. 20pthread_t pt_1 = 0;
  21. 21pthread_t pt_2 = 0;
  22. 22int ret = 0;
  23. 23PrivInfo* thiz = NULL;
  24. 24thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
  25. 25if (thiz == NULL)
  26. 26{
  27. 27printf ("[%s]: Failed to malloc priv./n");
  28. 28return -1;
  29. 29}
  30. 30info_init (thiz);
  31. 31ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);
  32. 32if (ret != 0)
  33. 33{
  34. 34perror ("pthread_1_create:");
  35. 35}
  36. 36ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);
  37. 37if (ret != 0)
  38. 38{
  39. 39perror ("pthread_2_create:");
  40. 40}
  41. 41pthread_join (pt_1, NULL);
  42. 42pthread_join (pt_2, NULL);
  43. 43info_destroy (thiz);
  44. 44return 0;
  45. 45}
  46. 46static void info_init (PrivInfo* thiz)
  47. 47{
  48. 48return_if_fail (thiz != NULL);
  49. 49thiz->end_time = time(NULL) + 10;
  50. 50sem_init (&thiz->s1, 0, 1);
  51. 51sem_init (&thiz->s2, 0, 0);
  52. 52return;
  53. 53}
  54. 54static void info_destroy (PrivInfo* thiz)
  55. 55{
  56. 56return_if_fail (thiz != NULL);
  57. 57sem_destroy (&thiz->s1);
  58. 58sem_destroy (&thiz->s2);
  59. 59free (thiz);
  60. 60thiz = NULL;
  61. 61return;
  62. 62}
  63. 63static void* pthread_func_1 (PrivInfo* thiz)
  64. 64{
  65. 65return_if_fail(thiz != NULL);
  66. 66while (time(NULL) < thiz->end_time)
  67. 67{
  68. 68sem_wait (&thiz->s2);
  69. 69printf ("pthread1: pthread1 get the lock./n");
  70. 70sem_post (&thiz->s1);
  71. 71printf ("pthread1: pthread1 unlock/n");
  72. 72sleep (1);
  73. 73}
  74. 74return;
  75. 75}
  76. 76static void* pthread_func_2 (PrivInfo* thiz)
  77. 77{
  78. 78return_if_fail (thiz != NULL);
  79. 79while (time (NULL) < thiz->end_time)
  80. 80{
  81. 81sem_wait (&thiz->s1);
  82. 82printf ("pthread2: pthread2 get the unlock./n");
  83. 83sem_post (&thiz->s2);
  84. 84printf ("pthread2: pthread2 unlock./n");
  85. 85sleep (1);
  86. 86}
  87. 87return;
  88. 88}
复制代码

#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> #define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;} typedef struct _PrivInfo { sem_t s1; sem_t s2; time_t end_time; }PrivInfo; static void info_init (PrivInfo* thiz); static void info_destroy (PrivInfo* thiz); static void* pthread_func_1 (PrivInfo* thiz); static void* pthread_func_2 (PrivInfo* thiz); int main (int argc, char** argv) { pthread_t pt_1 = 0; pthread_t pt_2 = 0; int ret = 0; PrivInfo* thiz = NULL; thiz = (PrivInfo* )malloc (sizeof (PrivInfo)); if (thiz == NULL) { printf ("[%s]: Failed to malloc priv./n"); return -1; } info_init (thiz); ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz); if (ret != 0) { perror ("pthread_1_create:"); } ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz); if (ret != 0) { perror ("pthread_2_create:"); } pthread_join (pt_1, NULL); pthread_join (pt_2, NULL); info_destroy (thiz); return 0; } static void info_init (PrivInfo* thiz) { return_if_fail (thiz != NULL); thiz->end_time = time(NULL) + 10; sem_init (&thiz->s1, 0, 1); sem_init (&thiz->s2, 0, 0); return; } static void info_destroy (PrivInfo* thiz) { return_if_fail (thiz != NULL); sem_destroy (&thiz->s1); sem_destroy (&thiz->s2); free (thiz); thiz = NULL; return; } static void* pthread_func_1 (PrivInfo* thiz) { return_if_fail(thiz != NULL); while (time(NULL) < thiz->end_time) { sem_wait (&thiz->s2); printf ("pthread1: pthread1 get the lock./n"); sem_post (&thiz->s1); printf ("pthread1: pthread1 unlock/n"); sleep (1); } return; } static void* pthread_func_2 (PrivInfo* thiz) { return_if_fail (thiz != NULL); while (time (NULL) < thiz->end_time) { sem_wait (&thiz->s1); printf ("pthread2: pthread2 get the unlock./n"); sem_post (&thiz->s2); printf ("pthread2: pthread2 unlock./n"); sleep (1); } return; }

分享到:

相关信息

  • 如何安装Ubuntu系统?Ubuntu系统的安装教程

    Ubuntu 是一个启动速度超快、界面友好、安全性好的开源操作系统,它由全球顶尖开源软件专家开发,适用于桌面电脑、笔记本电脑等,并且它可以永久免费使用。很多朋友都不知道要怎么安装Ubuntu系统,那Ubuntu系统要怎么安装?...

    2023-03-16

  • Linux系统怎么分析Nginx日志?linux系统日志管理教程

    Linux系统下Nginx 日志可以查看系统运行记录和出错说明,对Nginx 日志的分析可以了解系统运行的状态。那么Linux系统Nginx日志怎么分析呢?...

    2023-03-16

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载

公众号