2

For some reason, pthread_join always causes a SIGSEGV action on my computer when I run with Valgrind. To test this, I ran the following code from https://computing.llnl.gov/tutorials/pthreads/:

/* pthread.c */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4

void *BusyWork(void *t)
{
  int i;
  long tid;
  double result=0.0;
  tid = (long)t;
  printf("Thread %ld starting...\n",tid);
  for (i=0; i<1000000; i++)
  {
    result = result + sin(i) * tan(i);

  }
  printf("Thread %ld done. Result = %e\n",tid, result);
  pthread_exit((void*) t);

}

int main (int argc, char *argv[])
{
  pthread_t thread[NUM_THREADS];
  pthread_attr_t attr;
  int rc;
  long t;
  void *status;

  /* Initialize and set thread detached attribute */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  for(t=0; t<NUM_THREADS; t++) {
    printf("Main: creating thread %ld\n", t);
    rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t); 
    if (rc) {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);

    }

  }

  /* Free attribute and wait for the other threads */
  pthread_attr_destroy(&attr);
  for(t=0; t<NUM_THREADS; t++) {
    rc = pthread_join(thread[t], &status);
    if (rc) {
      printf("ERROR; return code from pthread_join() is %d\n", rc);
      exit(-1);

    }
    printf("Main: completed join with thread %ld having a status   of %ld\n",
        t,(long)status);

  }

  printf("Main: program completed. Exiting.\n");
  pthread_exit(NULL);
}

Running

$ clang -g -o pthread pthread.c
$ valgrind --leak-check=full ./pthread

Yields the following valgrind error:

==17283== Process terminating with default action of signal 11 (SIGSEGV)
==17283==  Access not within mapped region at address 0x700002F3CC3E
==17283==    at 0x10044437F: _pthread_find_thread (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100446D52: _pthread_join_cleanup (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100446C63: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100000D52: main (pthread.c:50)

My Computer is a Mac OS X (El Capitan). For clang, I am using Apple LLVM version 7.0.2 (clang-700.1.81). For Valgrind I am using valgrind-3.11.0.

ethanabrooks
  • 747
  • 8
  • 19
  • 1
    I'm having exactly the same problem with simple C++: http://stackoverflow.com/questions/36576251/stdthread-join-sigsegv-on-mac-os-under-valgrind – KevinD May 16 '16 at 08:17
  • Per KevinD's link, there's an official [bug report](https://bugs.kde.org/show_bug.cgi?id=349128) for this, but it may be an [issue](https://bytecoin.org/blog/pthread-bug-osx/) with libpthread. It also seems to be causing an assertion failure in `leak-check` mode on Mac, re: which [Philippe W.](http://www.valgrind.org/info/developers.html) has [stated](http://valgrind-users.narkive.com/LiWqf1G8/segfault-assertion-bad-scanned-addr-vg-roundup-mpi-pthread-program#post3) _"without a reproducer on linux, not much chance to to [sic] find the problem with mail remote debugging"_ –  Jan 09 '17 at 15:24

1 Answers1

1

I was unable to reproduce your problem on my Mac OS X Yosemite (10.10.5) with kernel 14.5.0. The Clang and Valgrind versions I use are the same as yours (please double check just in case):

$ clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

$ valgrind --version
valgrind-3.11.0

Thus the only difference is the OS version. Here are the steps I followed:

  1. Refreshed the installation of the developer tools by running xcode-select --install. Without doing this I could not build Valgrind.
  2. Set the CC and CXX environment variables to clangand clang++, respectively. I did this to ensure that Clang, not GCC, is used. Setting CXX was probably overkill. I later wiped out the Valgrind installation and re-did it with GCC, but it was still a success, even though I think I was getting more warnings.
  3. Did ./configure --prefix=/usr/local in the root of the untarred Valgrind tarball, followed by make and sudo make install.

Hopefully these steps help you resolve the issue.

Anatoli P
  • 4,791
  • 1
  • 18
  • 22
  • Thanks for the suggestion. I updated my post with the information you asked for. When I run the program without valgrind, I have no problems. – ethanabrooks Feb 25 '16 at 14:12