|
From: Bjoern D. <bjo...@go...> - 2011-12-16 11:00:54
|
> #include <pthread.h>
> #include<stdio.h>
> int food =0 ;
> pthread_mutex_t foodlock;
>
> int child_fn ( ) {
> pthread_mutex_lock(&foodlock);
> if (food>=0) {
> food++;
> }
> pthread_mutex_unlock(&foodlock);
> return food; //Read without lock ...Which may return with
> un-predictable value here ....
>
> }
>
>
> int main ( void ) {
> pthread_t child;
> pthread_create(&child, NULL, child_fn, NULL);
> pthread_join(child, NULL);
> return 0;
> }
>
>
> Did you guys find any datarace issue in the above code ,If so why is
> valgrind with helgrind or drd tool unable to find the same ..please find
> the below report from valgrind
Your program only runs the racy function in a single thread. There is
by definition no data race in such execution. A slight modification
yields the valid race error:
#include <pthread.h>
#include<stdio.h>
int food =0 ;
pthread_mutex_t foodlock;
void* child_fn ( void*) {
pthread_mutex_lock(&foodlock);
if (food>=0) {
food++;
}
pthread_mutex_unlock(&foodlock);
return (void*)food; //Read without lock ...Which may return
with un-predictable value here ....
}
int main ( void ) {
pthread_t child, child2;
pthread_create(&child, NULL, child_fn, NULL);
pthread_create(&child2, NULL, child_fn, NULL);
pthread_join(child, NULL);
pthread_join(child2, NULL);
return 0;
}
==30866== Helgrind, a thread error detector
==30866== Copyright (C) 2007-2011, and GNU GPL'd, by OpenWorks LLP et al.
==30866== Using Valgrind-3.8.0.SVN and LibVEX; rerun with -h for copyright info
==30866== Command: ./t
==30866==
==30866== ---Thread-Announcement------------------------------------------
==30866==
==30866== Thread #3 was created
==30866== at 0x413E0B8: clone (clone.S:111)
==30866==
==30866== ---Thread-Announcement------------------------------------------
==30866==
==30866== Thread #2 was created
==30866== at 0x413E0B8: clone (clone.S:111)
==30866==
==30866== ----------------------------------------------------------------
==30866==
==30866== Lock at 0x804A02C was first observed
==30866== at 0x402BAE6: pthread_mutex_lock (hg_intercepts.c:495)
==30866== by 0x8048515: child_fn(void*) (test.cc:7)
==30866== by 0x402B7B0: mythread_wrapper (hg_intercepts.c:219)
==30866== by 0x4057D30: s==30866== This conflicts with a previous
read of size 4 by thread #2
==30866== Locks held: none
==30866== at 0x8048538: child_fn(void*) (test.cc:12)
==30866== by 0x402B7B0: mythread_wrapper (hg_intercepts.c:219)
==30866== by 0x4057D30: start_thread (pthread_create.c:304)
==30866== by 0x413E0CD: clone (clone.S:130)
==30866==
==30866==
==30866== For counts of detected and suppressed errors, rerun with: -v
==30866== Use --history-level=approx or =none to gain increased speed, at
==30866== the cost of reduced accuracy of conflicting-access information
==30866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 9 from 9)
tart_thread (pthread_create.c:304)
==30866== by 0x413E0CD: clone (clone.S:130)
==30866==
==30866== Possible data race during write of size 4 at 0x804A028 by thread #3
==30866== Locks held: 1, at address 0x804A02C
==30866== at 0x8048527: child_fn(void*) (test.cc:9)
==30866== by 0x402B7B0: mythread_wrapper (hg_intercepts.c:219)
==30866== by 0x4057D30: start_thread (pthread_create.c:304)
==30866== by 0x413E0CD: clone (clone.S:130)
==30866==
|