系统屋 - 系统光盘下载网站!

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

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

时间:2019-08-24来源:系统屋作者: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 <cstdio>
  2. 02#include <cstdlib>
  3. 03#include <unistd.h>
  4. 04#include <pthread.h>
  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 <cstdio> #include <cstdlib> #include <unistd.h> #include <pthread.h> #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系统禁用ping命令的技巧

    Linux系统中的ping命令可用于网络故障的诊断,检查网络的连通性,但有时候会被黑客利用,有时为了保证系统网络的安全,会将ping命令禁止使用,那么要如何禁止ping命令呢?随小编一起来了解下吧。...

    2019-12-12

  • Linux将多个文件内容合成一个的方法

    Linux用户在进行多文件内容导入数据库的时候,发现过程繁琐,如果将多个文件内容合并成一个文件会简单很多,下面小编就给大家介绍下Linux将多个文件内容合成一个的方法,感兴趣的朋友可以来学习下。...

    2019-12-11

评论

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载

公众号