Hello,

I'm using helgrind quite a lot these days, and I love it.

However I wonder if it doesn't give me false positives for the case of reading 
a value from a static object, which was set in the constructor.

Given that gcc does indeed implement "threadsafe statics" as per C++11 (but 
even before C++11 came out), one can assume that gcc does "something like" a 
mutex around the creation of the object, and therefore that there is a 
"happens before" relation between the end of the constructor and the use of 
this object later on, right?

In that case it would seem that helgrind needs to learn that, to avoid many 
false positives.

Testcase attached.

The assembly code says
        call    __cxa_guard_acquire
        testl   %eax, %eax
        je      .L3
        .loc 1 16 0 discriminator 2
        movl    $_ZZ11threadStartPvE9singleton, %edi
        call    _ZN9SingletonC1Ev
        movl    $_ZGVZ11threadStartPvE9singleton, %edi
        call    __cxa_guard_release
.L3:

IIRC __cxa_guard_acquire/release is the protection around the static, but I'm 
not sure exactly what this means. Is there an actual happens-before relation 
here?

helgrind log:

==31469== Possible data race during read of size 4 at 0x602068 by thread #3
==31469== Locks held: none
==31469==    at 0x400ADF: threadStart(void*) (testcase_local_static.cpp:17)
==31469==    by 0x4C2D151: mythread_wrapper (hg_intercepts.c:233)
==31469==    by 0x4E3C0DA: start_thread (pthread_create.c:309)
==31469==    by 0x595B90C: clone (clone.S:111)
==31469== 
==31469== This conflicts with a previous write of size 4 by thread #2
==31469== Locks held: none
==31469==    at 0x400BC6: Singleton::Singleton() (testcase_local_static.cpp:9)
==31469==    by 0x400AD4: threadStart(void*) (testcase_local_static.cpp:16)
==31469==    by 0x4C2D151: mythread_wrapper (hg_intercepts.c:233)
==31469==    by 0x4E3C0DA: start_thread (pthread_create.c:309)
==31469==    by 0x595B90C: clone (clone.S:111)


-- 
David Faure, fa...@kde.org, http://www.davidfaure.fr
Working on KDE Frameworks 5
#include <pthread.h>
#include <stdio.h>

// gcc is supposed to have threadsafe statics

class Singleton
{
public:
    Singleton() : value(42) {}

    int value;
};

void * threadStart(void *)
{
    static Singleton singleton;
    printf("%d\n", singleton.value);
    printf("%d\n", singleton.value);
    printf("%d\n", singleton.value);
    printf("%d\n", singleton.value);
    printf("%d\n", singleton.value);
    return 0;
}

int main( int , char** ) {
    pthread_t thread1;
    if ( pthread_create(&thread1, 0, threadStart, 0) )
        return 1;
    pthread_t thread2;
    if ( pthread_create(&thread2, 0, threadStart, 0) )
        return 1;

    void* v;
    pthread_join(thread1, &v);
    pthread_join(thread2, &v);
    return 0;
}

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their 
applications. Written by three acclaimed leaders in the field, 
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to