Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 92184

Summary: strange app behaviour with NPTL enabled
Product: Gentoo Linux Reporter: barthek <gejzer>
Component: [OLD] Core systemAssignee: Gentoo Toolchain Maintainers <toolchain>
Status: RESOLVED WONTFIX    
Severity: normal    
Priority: High    
Version: 2005.0   
Hardware: All   
OS: All   
Whiteboard:
Package list:
Runtime testing required: ---

Description barthek 2005-05-10 12:35:32 UTC
i have this really simple threading application downloaded off the net to test threading issues. its behaviour has changed since i switched to NPTL. i have recompiled glibc with +nptl -nptlonly

take a look at this example and please tell me what you think about it and why the change occured (notice different char output):

doli@1g ~/src $ ./thread
o....................ooooooooooooooooooo
myglobal equals 40
doli@1g ~/src $ export LD_ASSUME_KERNEL=2.4.1
doli@1g ~/src $ ./thread
o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.
myglobal equals 40

here is the app:
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

void *thread_function(void *arg) {
  int i,j;
  for ( i=0; i<20; i++ ) {
    pthread_mutex_lock(&mymutex);
    j=myglobal;
    j=j+1;
    printf(".");
    fflush(stdout);
    sleep(1);
    myglobal=j;
    pthread_mutex_unlock(&mymutex);
  }
  return NULL;
}

int main(void) {

  pthread_t mythread;
  int i;

  if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
    printf("error creating thread.");
    abort();
  }

  for ( i=0; i<20; i++) {
    pthread_mutex_lock(&mymutex);
    myglobal=myglobal+1;
    pthread_mutex_unlock(&mymutex);
    printf("o");
    fflush(stdout);
    sleep(1);
  }

  if ( pthread_join ( mythread, NULL ) ) {
    printf("error joining thread.");
    abort();
  }

  printf("\nmyglobal equals %d\n",myglobal);

  exit(0);
}
Comment 1 SpanKY gentoo-dev 2005-05-10 16:09:07 UTC
i dont see anything wrong with the differences, logically the code could produce either right ?
Comment 2 barthek 2005-05-11 00:48:48 UTC
you are right, but what if i need this dummy pthreaded program
to output exactly the pattern i need ? (o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.o.)
i expect that if i mutex_lock() and then fflush() i would receive output above.

i played with placing fflush() between mutex_lock() and mutex_unlock() with different effects which were all different from the original behaviour

what would be the proper way of doing it ?
Comment 3 SpanKY gentoo-dev 2005-05-11 05:40:33 UTC
if you rely on threads to execute in a certain order and dont setup locks in order to achieve that, your code is broken already

so in this case, the code is broken if you need it to output in that exact order
Comment 4 barthek 2005-05-11 06:29:16 UTC
thanks for the comment and sorry for taking your time