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== 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 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进行写操作,不加锁,则可被检测出!
10-08 18:32