0

I have the following simple program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>

#define handle_error_en(en, msg) \
    {do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0);}
#define status_check(stat) ((stat != 0) ? (handle_error_en(stat, "status error")) : ((void)0))

static void* thread_start(void *arg)
{
    printf("%s\n", "thread working..");
    sleep(1);
    return NULL;
}

int main(int argc, char const *argv[])
{
    int status;
    pthread_t thread;

    status = pthread_create(&thread,
                        NULL,
                        thread_start,
                        NULL);
    status_check(status);
    status = pthread_join(thread, NULL);
    status_check(status);
    printf("%s\n", "exit program..");
    return 0;
}

When I run Valgind with

Valgrind --tool=helgrind ./threads_simple

I get 54 errors, all being data race errors. Just picking one of them:

Possible data race during write of size 4 at 0x1003FD2D8 by thread #1
==17334== Locks held: none
==17334==    at 0x1003E2FDF: spin_unlock (in /usr/lib/system/libsystem_platform.dylib)
==17334==    by 0x1003F7EAF: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==17334==    by 0x100000E37: main (threads_simple.c:29)
==17334== 
==17334== This conflicts with a previous write of size 4 by thread #2
==17334== Locks held: none
==17334==    at 0x1003E2FDF: spin_unlock (in /usr/lib/system/libsystem_platform.dylib)
==17334==    by 0x1003F6FD6: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib) 
==17334==    by 0x1003F43EC: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==17334==  Address 0x1003fd2d8 is in the Data segment of /usr/lib/system/libsystem_pthread.dylib

I have written many more small thread-programms, all give errors like this. Is my Valgrind installation corrupt? I installed it on mac via brew.

user2908112
  • 457
  • 6
  • 18
  • FYI: For your code, Helgrind reports no such races on Linux. – P.P Sep 21 '16 at 06:32
  • Ok, then it has to be something with the installation.. – user2908112 Sep 21 '16 at 06:34
  • Is this applicable to your platform? http://stackoverflow.com/questions/3941271/why-are-malloc-and-printf-said-as-non-reentrant – Klas Lindbäck Sep 21 '16 at 07:05
  • version 3.11.0 on Yosmite 10.10.5. Tried to install manually also, but same thing. On http://valgrind.org/downloads/current.html it says (Mac OS X 10.10, with limited support for 10.11) – user2908112 Sep 21 '16 at 09:49
  • This looks to me like Helgrind doesn't understand the code inside the pthreads implementation. Often, the implementation does things that would be wrong if you did them yourself, but the people who wrote the implementation know they can get away with it. So this is not a symptom of a _corrupt_ Valgrind, but of an incomplete port to OSX. – zwol Sep 21 '16 at 13:01
  • Can buy that. But how can they live with themselves, not completing their work? – user2908112 Sep 21 '16 at 14:20
  • Any call to `valgrind` should be parameterized to ignore problems found in the system functions as they have no effect on your program. – user3629249 Sep 22 '16 at 02:23
  • How do I parameterize a call to valgrind? – user2908112 Sep 22 '16 at 16:26

0 Answers0