http://blog.chinaunix.net/uid-14880649-id-2954431.html
1.Solaris .vs. Linux Posix 库
Solaris 库(lib 线程) Linux POSIX 库(libp 线程) 操作
sema_destroy() sem_destroy() 销毁信号状态。
sema_init() sem_init() 初始化信号。
sema_post() sem_post() 增加信号。
sema_wait() sem_wait() 阻止信号计数。
sema_trywait() sem_trywait() 减少信号计数。
mutex_destroy() pthread_mutex_destroy() 销毁或禁用与互斥对象相关的状态。
mutex_init() pthread_mutex_init() 初始化互斥变量。
mutex_lock() pthread_mutex_lock() 锁定互斥对象和块,直到互斥对象被释放。
mutex_unlock() pthread_mutex_unlock() 释放互斥对象。
cond_broadcast() pthread_cond_broadcast() 解除对等待条件变量的所有线程的阻塞。
cond_destroy() pthread_cond_destroy() 销毁与条件变量相关的任何状态。
cond_init() pthread_cond_init() 初始化条件变量。
cond_signal() pthread_cond_signal() 解除等待条件变量的下一个线程的阻塞。
cond_wait() pthread_cond_wait() 阻止条件变量,并在最后释放它。
rwlock_init() pthread_rwlock_init() 初始化读/写锁。
rwlock_destroy() pthread_rwlock_destroy() 锁定读/写锁。
rw_rdlock() pthread_rwlock_rdlock() 读取读/写锁上的锁。
rw_wrlock() pthread_rwlock_wrlock() 写读/写锁上的锁。
rw_unlock() pthread_rwlock_unlock() 解除读/写锁。
rw_tryrdlock() pthread_rwlock_tryrdlock() 读取非阻塞读/写锁上的锁。
rw_trywrlock() pthread_rwlock_trywrlock() 写非阻塞读/写锁上的锁。
  1. 如何在linux 下c++中类的成员函数中创建多线程
  2. linux系统中线程程序库是POSIX pthread。POSIX pthread它是一个c的库,用C语言进行多线程编程我这里就不多说了,网上的例子很多。但是如何在C++的类中实现多线程编程呢?如果套用C语言中创建多线程的方式,在编译的时候会出现...does not match `void*(*)(void*)..这样的错误。出现这种情况的原因是,编译器在处理C++和C文件上是不同的,也就是说C++和C语言里边指针函数不等价。解决这种错误的方法
  3. 有两种:
  4. 1、不要将线程函数定义为类的成员函数,但是在类的成员函数里边调用它。
  5. 例如:
  6. [test.h]
  7. #ifndef TEST_H
  8. #define TEST_H
  9. class test
  10. {
  11. public:
  12. test();
  13. ~test();
  14. private:
  15. void createThread();
  16. };
  17. #endif
  18. [test.cpp]
  19. test::test()
  20. {}
  21. test::~test()
  22. {}
  23. void *threadFunction()
  24. {
  25. printf("This is a thread");
  26. for(;;);
  27. }
  28. void test::createThread()
  29. {
  30. pthread_t threadID;
  31. pthread_create(&threadID, NULL, threadFunction, NULL);
  32. }
  33. [main.cpp]
  34. #inlcude "test.h"
  35. int main()
  36. {
  37. test t;
  38. t.createThead();
  39. for(;;);
  40. return 0;
  41. }
  42. 2、将线程函数作为类的成员函数,那么必须声明改线程函数为静态的函数,并且该线程函数所引用的其他成员函数也必须是静态的,如果要使用类的成员变量,则必须在创建线程的时候通过void *指针进行传递。
  43. 例如:
  44. 【test.h】
  45. #ifndef TEST_H
  46. #define TEST_H
  47. class test
  48. {
  49. public:
  50. test();
  51. ~test();
  52. private:
  53. int p;
  54. static void *threadFction(void *arg);
  55. static void sayHello(int r);
  56. void createThread();
  57. };
  58. #endif
  59. [test.cpp]
  60. test::test()
  61. {}
  62. test::~test()
  63. {}
  64. void *test::threadFunction(void *arg)
  65. {
  66. int m = *(int *)arg;
  67. sayHello(m);
  68. for(;;);
  69. }
  70. void sayHello(int r)
  71. {
  72. printf("Hello world %d!\n", r);
  73. }
  74. void test::createThread()
  75. {
  76. pthread_t threadID;
  77. pthread_create(&threadID, NULL, threadFunction, NULL);
  78. }
  79. [main.cpp]
  80. #inlcude "test.h"
  81. int main()
  82. {
  83. test t;
  84. t.createThead();
  85. for(;;);
  86. return 0;
  87. }
  88. ====================================================================================
  89. http://bigbossman.blogbus.com/logs/10761605.html
  90. Linux下的编程一直是C语言的天下,但老是用C感觉写的很乏味。用面向对象方法编程,听着都倍有面子。于是决定先在的这个项目用C++来写。虽然不一定能“以C++的思想”来写C++,少会有C++的样子。
  91. 但是问题来了:我需要在程序中动态创建一个线程,而pthread不接受C++类的成员函数作为参数。
  92. 原因也很简单,类成员是在类被实例化成为对象后才存在的,即在编译时是不存在的,编译器无法取得函数的确切入口地址,自然无法通过编译。
  93. 照这个分析,如果把要调用的类成员函数声明为静态的,pthread_create就可以找到函数的地址了。但这样一来的话,由于类中的静态函数无法调用类的非静态成员。线程函数的功能就受到了很大限制,也就没有比要将其放在类中了。
  94. 最容易想到的解决方案就是写个C函数,然后再C函数中调用C++对象的成员函数。
  95. 比如类为
  96. class foo(){
  97. public :
  98. thread();
  99. }
  100. class *f;
  101. void *bar(void *arg){
  102. f->thread();
  103. return NULL;
  104. }
  105. int main(){
  106. .........
  107. f=new foo();
  108. pthread_create(&tid,&tattr,bar,NULL);
  109. }
  110. 显然这种发法太笨了,而且对象只能是全局的。
  111. 注意到线程函数bar可以有一个任意类型的指针作为参数,那我们何不将对象通过这个指针将对象变为bar的一个参数,从而让我们的程序好看一些。
  112. class foo(){
  113. public :
  114. thread();
  115. }
  116. void *bar(void *args){
  117. foo *f=(foo *)args;
  118. f.thread();
  119. return NULL;
  120. }
  121. int main(){
  122. .........
  123. foo *f=new foo();
  124. pthread_create(&tid,&tattr,bar,f);
  125. }
  126. 如果把上述两种方法结合起来即对象中的静态函数+通过指针参数传递对象,那又会怎么样呢?
  127. class foo(){
  128. public :
  129. int thread();
  130. static void *wrapper(void *args){
  131. foo *f=static_cast<foo *>(args);
  132. f->thread();
  133. return NULL;
  134. }
  135. }
  136. int main(){
  137. .........
  138. foo *f=new foo();
  139. pthread_create(&tid,&tattr,foo::wrapper,&f);
  140. }
  141. 其他参考网页
  142. http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html
  143. http://www.cppblog.com/bigsml/archive/2006/09/07/12137.html

linux C++ 多线程编程相关推荐

  1. [转]Linux 的多线程编程的高效开发经验

    Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...

  2. Linux环境多线程编程基础设施

    Linux环境多线程编程基础设施 来源:Yebangyu 本文介绍多线程环境下并行编程的基础设施.主要包括: Volatile __thread Memory Barrier __sync_synch ...

  3. Linux 的多线程编程的高效开发经验

    背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文 ...

  4. linux线程多参数传递参数,Linux中多线程编程并传递多个参数

    解析Linux中多线程编程并传递多个参数 Linux中多线程编程并传递多个参数实例是本文讲解的内容,不多说,先来看内容. Linux下的多线程编程,并将多个参数传递给线程要执行的函数. 以下是实验程序 ...

  5. 对linux中多线程编程中pthread_join的理解

    对linux中多线程编程中pthread_join的理解 分类: 程序员面试 linux学习2013-08-04 21:32 234人阅读 评论(0) 收藏 举报 多线程linuxpthread_jo ...

  6. linux 多线程 semaphore ,Linux下多线程编程-Pthread和Semaphore使用.doc

    比锄戴垒丛共麦溺庄哆氏葫季袒飞闲棉铆稼椰悲倘寓矩案铺汞嫡懂伸腑箩五穗颗撩护尚巷苯宅瑚铱焕涅职枝怎摔什街杠写冻泡峡蠢舀以咽铝皇篮糠村墟凤帜攒摧定畜遁陛葛杯复妄婚赣续踌肖祷就抖帘荒徘魂圭焙酸劈待钞林讯啊铂 ...

  7. [原创]手把手教你Linux下的多线程设计--Linux下多线程编程详解(一)

    本文可任意转载,但必须注明作者和出处. [原创]手把手教你Linux下的多线程设计(一)                                       --Linux下多线程编程详解 原 ...

  8. Linux多进程多线程编程笔记

    文章目录 Linux多进程多线程编程 一.多进程编程 1.fork 函数 2.exec 系列函数 3.wait.waitpid 函数 4.pipe 管道 5.信号量 5.1.semget 5.2.se ...

  9. 【linux】多线程编程(c语言编程)

    多线程编程 一.线程的基本概念         与进程相比,多线程是一种非常"节俭"的多任务操作方式.在linux操作系统下,启动一个新进程必须给     它分配独立的地址空间,建 ...

最新文章

  1. 手机web——自适应网页设计(html/css控制) - 51CTO.COM
  2. endnote初始化数据库支持_5 个免费的在线 SQL 数据库环境,比Navicat 香
  3. 解决项目莫名奇妙的报错问题
  4. My Brute(HDU-3315)
  5. 自动与时间服务器时间同步,Windows系统时间同步(附时间同步服务器地址)
  6. [qq机器人] nonebot2 群管插件
  7. Linux三剑客之SED
  8. [cf] Deltix Round, Autumn 2021 A. Divide and Multiply
  9. 用友U8供应链期初数据录入案例教程2
  10. 【matlab】matlab相关系数计算公式(Pearson和Spearman,以及Kendall Rank)
  11. 广式粤语VS港式粤语
  12. 构建maven时No archetypes currently available的解决方法
  13. 基于java web的学生考勤带请假管理系统-计算机毕业设计
  14. 添加tomcat服务
  15. 2021年中国非金属3D打印市场趋势报告、技术动态创新及2027年市场预测
  16. 软件工程未来发展方向
  17. 甲骨文华育兴业|【大数据调查】80%的程序员年薪都在10万以上,三分之一的人年薪20万以上
  18. 以管理员身份运行打不开的软件
  19. 链塔智库|区块链产业要闻及动态周报(2020年8月第二周)
  20. 【array-java】531. Lonely Pixel I

热门文章

  1. mysql 5.6 缓存_为什么默认情况下从MySQL 5.6开始禁用query_cache_type?
  2. ubuntu导入第三方库_ubuntu用户切换、配置Python开发环境与所需的第三方库
  3. sql 分组求和_从零学会SQL:汇总分析 D3
  4. java 汉字排序_java实现中文汉字的首字母排序
  5. Elasticsearch 节点发现
  6. Flink Operator之CoGroup、Join以及Connect
  7. 自然语言处理之长短时记忆网络(六)
  8. 三十六、数据仓库的实现
  9. 关于Python异常处理,你需要了解的知识点
  10. Nature:压榨学生,论资排辈,现行论文作者制度已死