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

当前位置:计算机之家 > 系统教程 > Linux教程 > 线程同步的方法有哪些?Linux下实现线程同步的三种方法

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

时间:2017-10-26 来源:计算机之家

  线程同步的方法有哪些?在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
  2. 02#include
  3. 03#include
  4. 04#include
  5. 05#include "iostream"
  6. 06using namespace std;
  7. 07pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  8. 08int tmp;
  9. 09void* thread(void *arg)
  10. 10{
  11. 11cout << "thread id is " << pthread_self() << endl;
  12. 12pthread_mutex_lock(&mutex);
  13. 13tmp = 12;
  14. 14cout << "Now a is " << tmp << endl;
  15. 15pthread_mutex_unlock(&mutex);
  16. 16return NULL;
  17. 17}
  18. 18int main()
  19. 19{
  20. 20pthread_t id;
  21. 21cout << "main thread id is " << pthread_self() << endl;
  22. 22tmp = 3;
  23. 23cout << "In main func tmp = " << tmp << endl;
  24. 24if (!pthread_create(&id, NULL, thread, NULL))
  25. 25{
  26. 26cout << "Create thread success!" << endl;
  27. 27}
  28. 28else
  29. 29{
  30. 30cout << "Create thread failed!" << endl;
  31. 31}
  32. 32pthread_join(id, NULL);
  33. 33pthread_mutex_destroy(&mutex);
  34. 34return 0;
  35. 35}
  36. 36//编译:g++ -o thread testthread.cpp -lpthread
复制代码
#include #include #include #include #include "iostream"using namespace std;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int tmp;void* thread(void *arg){cout << "thread id is " << pthread_self() << endl;pthread_mutex_lock(&mutex);tmp = 12;cout << "Now a is " << tmp << endl;pthread_mutex_unlock(&mutex);return NULL;}int main(){pthread_t id;cout << "main thread id is " << pthread_self() << endl;tmp = 3;cout << "In main func tmp = " << tmp << endl;if (!pthread_create(&id, NULL, thread, NULL)){cout << "Create thread success!" << endl;}else{cout << "Create thread failed!" << endl;}pthread_join(id, NULL);pthread_mutex_destroy(&mutex);return 0;}//编译:g++ -o thread testthread.cpp -lpthread
本文标题:线程同步的方法有哪些?Linux下实现线程同步的三种方法 - Linux教程
本文地址:www.suan5.cn/xtjc/555.html

相关信息

  • Ubuntu 13.10不能启动VirtualBox怎么办?

    我们都知道VirtualBox是一款虚拟机,能够在Ubuntu系统中运行,但在Ubuntu 13.10运行的时候提示错误,导致VirtualBox无法启动,遇到这个问题怎么办?下面小编就给大家介绍下Ubuntu13.1...

    2019/08/28

  • Linux系统如何安装配置Sendmail?

    Sendmail是目前在网络上最受欢迎的一种E-mail服务器,在互联网上是广受好评,很多企业用户都在使用。那么,Linux系统如何安装配置Sendmail呢?下面,我们就一起往下看看Linux系统安装...

    2018/12/19

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载

公众号