code:
-
#include<pthread.h>
-
#include<unistd.h>
-
static int __val = 0;
-
static void* thread_fun1(void *__arg)
-
{
-
while(true)
-
{
-
++__val;
-
}
-
return NULL;
-
}
-
-
static void* thread_fun2(void *__arg)
-
{
-
while(true)
-
{
-
++__val;
-
}
-
return NULL;
-
}
-
-
int main()
-
{
-
pthread_t __thread_id1;
-
pthread_t __thread_id2;
-
int __res1 = pthread_create(&__thread_id1,NULL,&thread_fun1,NULL);
-
int __res2 = pthread_create(&__thread_id2,NULL,&thread_fun2,NULL);
-
sleep(1);
-
return 0;
-
}
usage:
-
valgrind --tool=helgrind --log-file=helgrind.log ./helgrind_test
output:
-
==2268== Helgrind, a thread error detector
-
==2268== Copyright (C) 2007-2010, and GNU GPL'd, by OpenWorks LLP et al.
-
==2268== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
-
==2268== Command: ./helgrind_test
-
==2268== Parent PID: 1974
-
==2268==
-
==2268== Thread #3 was created
-
==2268== at 0x5131F4E: clone (clone.S:77)
-
==2268== by 0x4E36E7F: do_clone.constprop.3 (createthread.c:75)
-
==2268== by 0x4E38604: pthread_create@@GLIBC_2.2.5 (createthread.c:256)
-
==2268== by 0x4C29B23: pthread_create_WRK (hg_intercepts.c:257)
-
==2268== by 0x4C29CA7: pthread_create@* (hg_intercepts.c:288)
-
==2268== by 0x400616: main (helgrind_test.cc:27)
-
==2268==
-
==2268== Thread #2 was created
-
==2268== at 0x5131F4E: clone (clone.S:77)
-
==2268== by 0x4E36E7F: do_clone.constprop.3 (createthread.c:75)
-
==2268== by 0x4E38604: pthread_create@@GLIBC_2.2.5 (createthread.c:256)
-
==2268== by 0x4C29B23: pthread_create_WRK (hg_intercepts.c:257)
-
==2268== by 0x4C29CA7: pthread_create@* (hg_intercepts.c:288)
-
==2268== by 0x4005F8: main (helgrind_test.cc:26)
-
==2268==
-
==2268== Possible data race during read of size 4 at 0x601038 by thread #3
-
==2268== at 0x4005C5: thread_fun2(void*) (helgrind_test.cc:17)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268== This conflicts with a previous write of size 4 by thread #2
-
==2268== at 0x4005B5: thread_fun1(void*) (helgrind_test.cc:8)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268==
-
==2268== Possible data race during write of size 4 at 0x601038 by thread #3
-
==2268== at 0x4005CE: thread_fun2(void*) (helgrind_test.cc:17)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268== This conflicts with a previous write of size 4 by thread #2
-
==2268== at 0x4005B5: thread_fun1(void*) (helgrind_test.cc:8)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268==
-
==2268== Possible data race during read of size 4 at 0x601038 by thread #2
-
==2268== at 0x4005AC: thread_fun1(void*) (helgrind_test.cc:8)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268== This conflicts with a previous write of size 4 by thread #3
-
==2268== at 0x4005CE: thread_fun2(void*) (helgrind_test.cc:17)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268==
-
==2268== Possible data race during write of size 4 at 0x601038 by thread #2
-
==2268== at 0x4005B5: thread_fun1(void*) (helgrind_test.cc:8)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268== This conflicts with a previous write of size 4 by thread #3
-
==2268== at 0x4005CE: thread_fun2(void*) (helgrind_test.cc:17)
-
==2268== by 0x4C29C90: mythread_wrapper (hg_intercepts.c:221)
-
==2268== by 0x4E37EFB: start_thread (pthread_create.c:304)
-
==2268== by 0x5131F8C: clone (clone.S:112)
-
==2268==
-
==2268==
-
==2268== For counts of detected and suppressed errors, rerun with: -v
-
==2268== Use --history-level=approx or =none to gain increased speed, at
-
==2268== the cost of reduced accuracy of conflicting-access information
-
==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进行写操作,不加锁,则可被检测出!
阅读(3510) | 评论(0) | 转发(0) |