Search code examples
cmultithreadingfor-looppthreadspthread-join

pthread_join seems to modify my loop index


My code (see below) produces an odd behaviour. The output is:

Testing whether there are problems with concurrency ...rc is 0. i is 0
.rc is 0. i is 0
.rc is 3. i is 1
.rc is 0. i is 0
.rc is 3. i is 1
.rc is 3. i is 2
.rc is 0. i is 0
.rc is 3. i is 1
.rc is 3. i is 2
.rc is 3. i is 3
.rc is 0. i is 0
.rc is 3. i is 1
.rc is 3. i is 2
.rc is 3. i is 3
.rc is 3. i is 4
.rc is 0. i is 0
Segmentation fault (core dumped)

I tried to debug it, but only found out that i is reset to 0 right after pthread_join. This leads me to the conclusion that the modification must happen somewhere there. But i can't find a thing. I feel kind of stupid, since this isn't really a hard piece of code. What did i not notice?

Operating system is Ubuntu 14.04. N_THREADS is currently set to 10, N_RUNS is 10000.

Main thread:

pthread_t threads[N_THREADS];
pthread_attr_t attr;
int i;
int rc;
int status;

printf("Testing whether there are problems with concurrency ...");

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

for (i = 0; i < N_THREADS; i++){
  if (i) {
    rc = pthread_create(&(threads[i]), &attr, addRemove, 0);
  } else {
    rc = pthread_create(&(threads[i]), &attr, readStuff, 0);
  }
  if (rc) return rc;
}

for(i = 0; i < N_THREADS; i++) {
  rc = pthread_join(threads[i], (void*) &status);
//  if(rc == 3) 
      printf("rc is %d. i is %d\n", rc, i);
//  if (rc) return rc;
  if (status) return status;
  printf(".");
}

pthread_attr_destroy(&attr);

return 0;

Worker threads:

void* readStuff(void* a)
{
  int i;
  for (i = 0; i< N_RUNS; i++){
  ;
  }
  pthread_exit((void*)0);
}

void* addRemove(void* a)
{
   int i;
   for (i = 0; i< N_RUNS; i++){
   ;
   }
   pthread_exit((void*)0);
}

There are no other threads except the main thread and the ones created in the code above.

Compileable example


Solution

  • I think your problem is with the pthread_join. From the man page:

           int pthread_join(pthread_t thread, void **retval);
           ...
    
           If  retval is not NULL, then pthread_join() copies the exit status of the tar‐
           get  thread  (i.e.,  the  value   that   the   target   thread   supplied   to
           pthread_exit(3))  into  the  location  pointed  to  by *retval.  If the target
           thread was canceled, then PTHREAD_CANCELED is placed in *retval.
    

    Note that it takes a void **, which means it overwrites the thing pointed to by retval with a void * (size 8 on 64 bit). You are passing an int * (i.e. &status), which is a pointer to an object of size 4 on most platforms.

    So, pthread_join will be overwriting memory. Instead, declare status as a void * as per the function prototype.

    You are also testing status; I don't know what you are trying to achieve here.

    In general, compiling with -Wall will show you these errors.