linux下valgrind检测跨线程访问-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 684108
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c ,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: c/c

2013-04-13 11:13:32

code:  

点击(此处)折叠或打开

  1. #include<pthread.h>
  2. #include<unistd.h>
  3. static int __val = 0;
  4.  static void* thread_fun1(void *__arg)
  5.  {
  6.     while(true)
  7.     {
  8.         __val;
  9.     }
  10.     return null;
  11.  }
  12.  
  13.   static void* thread_fun2(void *__arg)
  14.  {
  15.     while(true)
  16.     {
  17.         __val;
  18.     }
  19.     return null;
  20.  }

  21. int main()
  22. {
  23.      pthread_t __thread_id1;
  24.      pthread_t __thread_id2;
  25.     int __res1 = pthread_create(&__thread_id1,null,&thread_fun1,null);
  26.     int __res2 = pthread_create(&__thread_id2,null,&thread_fun2,null);
  27.     sleep(1);
  28.     return 0;
  29. }
usage:
  1. valgrind --tool=helgrind --log-file=helgrind.log ./helgrind_test
output:

点击(此处)折叠或打开

  1. ==2268== helgrind, a thread error detector
  2. ==2268== 凯发app官方网站 copyright (c) 2007-2010, and gnu gpl'd, by openworks llp et al.
  3. ==2268== using valgrind-3.6.1-debian and libvex; rerun with -h for 凯发app官方网站 copyright info
  4. ==2268== command: ./helgrind_test
  5. ==2268== parent pid: 1974
  6. ==2268==
  7. ==2268== thread #3 was created
  8. ==2268== at 0x5131f4e: clone (clone.s:77)
  9. ==2268== by 0x4e36e7f: do_clone.constprop.3 (createthread.c:75)
  10. ==2268== by 0x4e38604: pthread_create@@glibc_2.2.5 (createthread.c:256)
  11. ==2268== by 0x4c29b23: pthread_create_wrk (hg_intercepts.c:257)
  12. ==2268== by 0x4c29ca7: pthread_create@* (hg_intercepts.c:288)
  13. ==2268== by 0x400616: main (helgrind_test.cc:27)
  14. ==2268==
  15. ==2268== thread #2 was created
  16. ==2268== at 0x5131f4e: clone (clone.s:77)
  17. ==2268== by 0x4e36e7f: do_clone.constprop.3 (createthread.c:75)
  18. ==2268== by 0x4e38604: pthread_create@@glibc_2.2.5 (createthread.c:256)
  19. ==2268== by 0x4c29b23: pthread_create_wrk (hg_intercepts.c:257)
  20. ==2268== by 0x4c29ca7: pthread_create@* (hg_intercepts.c:288)
  21. ==2268== by 0x4005f8: main (helgrind_test.cc:26)
  22. ==2268==
  23. ==2268== possible data race during read of size 4 at 0x601038 by thread #3
  24. ==2268== at 0x4005c5: thread_fun2(void*) (helgrind_test.cc:17)
  25. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  26. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  27. ==2268== by 0x5131f8c: clone (clone.s:112)
  28. ==2268== this conflicts with a previous write of size 4 by thread #2
  29. ==2268== at 0x4005b5: thread_fun1(void*) (helgrind_test.cc:8)
  30. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  31. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  32. ==2268== by 0x5131f8c: clone (clone.s:112)
  33. ==2268==
  34. ==2268== possible data race during write of size 4 at 0x601038 by thread #3
  35. ==2268== at 0x4005ce: thread_fun2(void*) (helgrind_test.cc:17)
  36. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  37. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  38. ==2268== by 0x5131f8c: clone (clone.s:112)
  39. ==2268== this conflicts with a previous write of size 4 by thread #2
  40. ==2268== at 0x4005b5: thread_fun1(void*) (helgrind_test.cc:8)
  41. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  42. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  43. ==2268== by 0x5131f8c: clone (clone.s:112)
  44. ==2268==
  45. ==2268== possible data race during read of size 4 at 0x601038 by thread #2
  46. ==2268== at 0x4005ac: thread_fun1(void*) (helgrind_test.cc:8)
  47. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  48. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  49. ==2268== by 0x5131f8c: clone (clone.s:112)
  50. ==2268== this conflicts with a previous write of size 4 by thread #3
  51. ==2268== at 0x4005ce: thread_fun2(void*) (helgrind_test.cc:17)
  52. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  53. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  54. ==2268== by 0x5131f8c: clone (clone.s:112)
  55. ==2268==
  56. ==2268== possible data race during write of size 4 at 0x601038 by thread #2
  57. ==2268== at 0x4005b5: thread_fun1(void*) (helgrind_test.cc:8)
  58. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  59. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  60. ==2268== by 0x5131f8c: clone (clone.s:112)
  61. ==2268== this conflicts with a previous write of size 4 by thread #3
  62. ==2268== at 0x4005ce: thread_fun2(void*) (helgrind_test.cc:17)
  63. ==2268== by 0x4c29c90: mythread_wrapper (hg_intercepts.c:221)
  64. ==2268== by 0x4e37efb: start_thread (pthread_create.c:304)
  65. ==2268== by 0x5131f8c: clone (clone.s:112)
  66. ==2268==
  67. ==2268==
  68. ==2268== for counts of detected and suppressed errors, rerun with: -v
  69. ==2268== use --history-level=approx or =none to gain increased speed, at
  70. ==2268== the cost of reduced accuracy of conflicting-access information
  71. ==2268== error summary: 7066 errors from 4 contexts (suppressed: 0 from 0)

anysis
从输出结果看出,检测到了竞态条件,23~32行,
possible data race during read of size 4 at 0x601038 by thread #3,
this conflicts with a previous write of size 4 by thread #2
分别对应于代码
helgrind_test.cc:17,helgrind_test.cc:8
__val 为全局变量,同时被thread 2,3进行写操作,不加锁,则可被检测出!
阅读(6554) | 评论(2) | 转发(4) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~

yuyunliuhen2013-04-14 21:43:32

:这个工具不错,可以检测多线程访问的竞争条件的冲突,功能很强大, 要是能提示出哪个变量,或者哪段代码就牛逼了。

基本上能定位到产生问题的函数,以及所在文件的行,一看代码就能马上确定了

|

2013-04-14 12:37:39

这个工具不错,可以检测多线程访问的竞争条件的冲突,功能很强大, 要是能提示出哪个变量,或者哪段代码就牛逼了。

|
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图